zoukankan      html  css  js  c++  java
  • Day01_课后练习题

    1.(将摄氏温度转化华氏温度)编写一个从控制台读取摄氏温度并将他转变为华氏温度并予以显示的程序。转换公式如下。

    Fahrenheit = (9 / 5) *  celsius + 32

    这里是这个程序的实例运行。

    Enter a degree in Celsius:43
    43 Celsius is 109.4 Fahrenheit
    代码:

    celsius = float(input('Enter a degree in Celsius:'))
    fahrenheit = (9.0 / 5.0) * celsius + 32
    print(' %.0f Celsius is %.1f Fahrenheit '%(celsius,fahrenheit))

    运行结果:

    2.(计算圆柱体的体积)编写一个读取圆柱的半径和高并利用下面的公式计算圆柱体底面积和体积的程序:

    area = radius * radius * pi

    volume = area * length

    运行实例:

    运行代码:

    import math
    radius,length = eval(input('Enter the radius and length of a cylinder:'))
    area = radius ** 2 * math.pi
    volume = area * length
    print('The area is %.4f'%area)
    print('The volume is %.1f'%volume)

    运行结果:

    3.(将英尺数转换为米数)编写一个程序,读取英尺数然后将他转换成米数并显示。一英尺等于0.305米。

     运行代码:

    feet = float(input('Enter a value for feet:'))
    meter = feet * 0.305
    print('%.1f feet is %.4f meters'%(feet,meter))

    运行结果:

    4.(计算能量)编写一个程序,计算将水从初始温度加热到最终温度所需要的能量。输入一千克计算的水的重量以及初始温度和最终温度。公式:Q = M * (finaltem - inittem)* 4184

    运行代码:

    kilograms = float(input('Enter the amount of water kilograms:'))
    inittem = float(input('Enter the initial temperature:'))
    finaltem = float(input('Enter the final temperature:'))
    Q = kilograms * (finaltem - inittem) * 4184
    print('The energy needed is %.1f'%Q)

    运行结果:

    5.计算利息,如果知道差额和百分比的年利率,可以使用下面的公式计算下个月月供的利息。

                     利息 = 差额 * (年利率 / 1200)

    编写程序读取差额和年利率,计算利息。

    运行代码:

    balance,rate = eval(input('Enter balance and interest rate (e.g.,3 for 3%):'))
    interest = balance * (rate / 1200)
    print('The interest is %.5f'%interest)

    运行结果:

    6.计算平均加速度

    运行代码:

    v0,v1,t = eval(input('Enter v0,v1 and t:'))
    a = (v1-v0) / t
    print('The average acceleration is %.4f'%a)

    运行结果:

    7. 

    代码:

    amount = input('Enter the monthly saving amount:')
    sum = 0
    for i in range(6):
        sum = (sum+float(amount)) * (1 + 0.00417)
    print('After the sixth month,the account value is %.2f'%sum)

    运行结果:

    8.

    运行代码:

    num = int(input("Enter a number between 0 and 1000:"))
    if num < 0 and num > 1000:
        print('输入有误')
    else:
        a = int(num % 100)
        b = a % 10 # 百位数
        c = int(a / 10) # 十位数
        d = int(num / 100) # 个位数
        sum = b + c + d
    print('The sum of the digits is %d'%sum)

    运行结果:



  • 相关阅读:
    设置debian6源
    debian7编译安装tengine添加lua和ldap模块
    elasticsearch5使用snapshot接口备份索引
    logstash5生成init脚本后台启动
    Xpack集成LDAP
    debian安装filebeat5.5收集nginx日志
    kibana5画图
    安装Xtrabackup,设置定时备份msyql数据库
    编译安装nrpe,配置监控mysql端口和主从状态
    编译安装keepalived,实现双主mysql高可用
  • 原文地址:https://www.cnblogs.com/KAJIA1/p/11272137.html
Copyright © 2011-2022 走看看