1.摄氏温度转换为华氏温度:
#编写一个从控制台读取摄氏温度并将它转换为华氏温度并予以显示
celsius = eval(input("Enter a degree in Celsius:"))
fahrenheit = (9 / 5) * celsius + 32
print("%.2f Celsius is %.2f Fahrenheit" % (celsius,fahrenheit))
2.计算圆柱体的体积:
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 ", area)
print("the volume is ", volume)
3.0-1000之间的整数求和:
# 123 % 10 = 3 n1
# 123 // 10 = 12
# 12 % 10 =2 n2
# 12 // 10 = 1
# 1 % 10 = 1 n3
# 1 // 10 = 0
num = eval(input("enter a number between 0 and 1000:"))
n1 = num % 10
num = num // 10
n2 = num % 10
num //= 10
n3 = num % 10
sum = n1 + n2 + n3
print("the sum of the didits is", sum)
4.计算年数和天数:
'''
分钟数 // 60 = 小时数
小时数 // 24 = 天数
天数 // 365 = 年数
天数 % 365 = 不满一年多余出来的天数
'''
minutes = eval(input("enter the number of minutes:"))
hours = minutes // 60
days = hours // 24
years = days // 365
days = days % 10
print("%d minutes is approximately %d years and %d days" %(minutes,years,days))
5.指定范围内产生一个随机浮点数:
import random
a = 10
b = 20
c = random.random() * (b-a) + a
print(c)