[Python] Übungen --- Grundlegende Grammatik

 

#Demo 01
# 数据: 华氏温度 摄氏温度
# 指令
#1、提示用户输入摄氏温度
#2、利用已知公式,将摄氏温度转换为华氏温度
#3、输出两者的值
cel=float(input("Enter a degree in Celsious:"))
fah=(9 / 5)*cel + 32
print("%.0f Celsious is %.1f Fahrenheit"%(cel,fah))

 Testergebnisse:

 

"""
Demo 02
数据:圆柱半径和高,圆柱体底面积和体积
步骤:
1、提示用户输入圆柱的半径和高
2、计算圆柱体底面积
3、计算圆柱体体积
4、输出圆柱体底面积和体积
"""
radious,length=eval(input("Enter the radious and the length of a cylinder:"))
PI = 3.1415926
area = radious * radious * PI
volume = area * length
print("The area is %.4f" % area)
print("The volume is %.1f" % volume)

Testergebnisse:

 

 

#Demo 03
#数据:输入一个三位数,求 个位 十位 百位的和
#步骤:
#1、输入一个不超过三位数字num=123
#2、提取个位   num1=num%10
#3、去掉个位   num=num//10
#4、提取十位   num2=num%10
#5、去掉十位   num=num//10
#6、提取百位   num3=num%10
#7、输出三位的和

num = int(input("请输入一个三位数:"))
num1 = num % 10
num = num // 10
num2 = num % 10
num = num //10
num3 = num % 10
total = (num1 + num2 + num3)
print("The sum of the digits is " + str(total))

Testergebnisse:

 

 

"""
Demo 04
数据:分钟数转化为年和天
步骤:
1、提示用户输入分钟数
2、hours=minutes//60
3、days=hours//24
4、years=days//365
"""
minutes = int(input("Enter the number of minutes:"))
hours = minutes // 60
days = hours // 24
years = days // 365
surplus = days % 365
print("%d mintues is approximately %d years and %d days" %(minutes,years,surplus))

Testergebnisse:

 

 

"""
Demo 05
数据:初始摄氏温度,最终摄氏温度,热量
步骤:
1、提示用户输入水量、水的初始温度和最终温度
2、计算将水从初始温度加热到最终温度所需要的能量
3、输出所需要的能量
"""
M = float(input("Enter the amount of water in kilograms:"))
initial = float(input("Enter the intital temperature:"))
final = float(input("Enter the final temperature:"))
Q = M * (final - initial) * 4184
print("The energy needed is %.1f " % Q)

Testergebnisse:

 

 

 

"""
Demo 06
数据:华氏温度,风速,求风寒温度
步骤:
1、提示用户输入一个华氏度和一个风速
2、计算风寒温度
3、输出风寒温度
"""
t1 = float(input("Enter the temperature in Fahrenheit between -58 and 41:"))
v = float(input("Enter the wind speed in miles per hours:"))
t = 35.74 + 0.6215 * t1 -35.75 * (v**0.16) + 0.4275 * t1 * (v**0.16)
print("The wind chill index is %.5f" % t) 

Testergebnisse:

 

"""
Demo 07
数据:飞机加速度,起飞速度,求最短跑道速度
步骤:
1、提示用户输入飞机起飞速度和飞机加速度
2、计算最短跑道速度
3、输出最短跑道速度
"""
v,a = eval(input("Enter speed and acceleration:"))
length = v**2 / (2 * a)
print("The minimum runway length for this airplane is %.3f meter" % length)

Testergebnisse:

 

"""Demo 08
数据:一个四位整数
步骤:
1、num取余提取个位
2、num去掉个位
3、num取余提取十位
4、num去掉十位
5、num取余提取百位
6、num去掉百位
7、num取余提取千位
"""
num = eval(input("Enter an integer:"))
num1 = num % 10
num = num // 10
num2 = num % 10
num = num //10
num3 = num % 10
num = num // 10
num4 = num % 10
print(num1)
print(num2)
print(num3)
print(num4)

 Testergebnisse:

 

#Demo 09
#数据:三个点的坐标 x1,y1,x2,y2,x3,y3
#步骤:
#1、提示用户输入三个顶点坐标
#2、计算三边边长
#3、计算半周长s
#4、计算面积
x1,y1,x2,y2,x3,y3 = eval(input("请输入三个顶点坐标:"))
side1=((x1-x2)**2+(y1-y2)**2)**0.5
side2=((x1-x3)**2+(y1-y3)**2)**0.5
side3=((x2-x3)**2+(y2-y3)**2)**0.5
s=(side1+side2+side3)/2
area=(s*(s-side1)*(s-side2)*(s-side3))**0.5
print("the area of the triangle is %.1f" %area)

Testergebnisse:

 

"""
Demo 10
数据:给出边长,求面积
步骤:
1、提示用户输入正六边形的边长
2、写出面积公式
3、输出正六边形面积
"""
side = eval(input("Enter the sides:"))
area = 3 * 3**0.5 / 2 * side ** 2
print("The area of the hexagon is %.4f" % area)

Testergebnisse:

 

"""
Demo 11
数据:
步骤:
1、将历元时间获得的秒数取整成总秒数 total_seconds
2、先计算当前的秒数cur_seconds = total_seconds % 60
3、计算总分钟数total_minutes = total_seconds // 60
4、计算当前分钟数cur_minutes = total_minutes % 60
5、计算总小时数total_hours = total_minutes // 60
6、计算当前小时数cur_hours = total_hours % 24
"""
import time
offset = int(input("Enter the time zone offsets to GMT:"))

total_seconds = int(time.time())
cur_seconds = total_seconds % 60

total_minutes = total_seconds // 60
cur_minutes = total_minutes % 60

total_hours = total_minutes // 60
cur_hours = (total_hours % 24 + offset) % 24

print("The current time is ",cur_hours,":",cur_minutes,":",cur_seconds)

Testergebnisse:

 

"""
Demo 12
数据:知每月存款金额saving,求六个月后账户总金额value
步骤:
1、提示用户输入每月存款金额
2、依据公式计算六个月后总金额
3、输出六个月后总金额
"""
saving = eval(input("Tnter the nothly saving amount:"))
value1 = saving *(1 + 0.00417)
value2 = (saving + value1) * (1 + 0.00417)
value3 = (saving + value2) * (1 + 0.00417)
value4 = (saving + value3) * (1 + 0.00417)
value5 = (saving + value4) * (1 + 0.00417)
value = (saving + value5) * (1 + 0.00417)
print("After the sixth month,the account value is %.2f" %value)

 Testergebnisse:

 

"""
Demo 13
数据:投资额investment,投资率rate,年数years,求未来投资金额value
步骤:
1、提示用户分别输入投资金额,投资率,年数
2、通过年利率求月利率
3、年数转化为月数
4、根据公式计算未来投资额
5、输出未来投资金额
"""
investment = eval(input("Enter investment amount:"))
rate = eval(input("Enter annual insterest rate:"))
years = eval(input("Enter number of years:"))
rate *=  0.01 / 12
months = years * 12
value = investment *(1 + rate) ** months
print("Accumulated value is %.2f" % value)

Testergebnisse:

 

"""
Demo 14
数据:知三角形三边a,b,c 求三个角A,B,C
步骤:
1、提示用户输入三条边
2、按公式计算三个角
3、转换为角度
"""
import math
a,b,c = eval(input("请输入三角形的三条边:"))
A = math.acos((a * a - b * b - c * c) / (-2 *b * c))
B = math.acos((b * b - a * a - c * c) / (-2 *a * c))
C = math.acos((c * c - a * a - b * b) / (-2 *a * b))
A=round((math.degrees(A)))
B=round((math.degrees(B)))
C=round((math.degrees(C)))
print("A=%d°,B=%d°,C=%d°" %(A,B,C))

Testergebnisse:

 

"""
Demo 15
数据:正n边形边长s,求正多边形面积area
步骤:
1、提示用户分别输入边数、边长
2、根据公式计算面积
3、输出面积
"""
import math
n = eval(input("Enter the number of sides:"))
s = eval(input("Enter the side:"))
t = math.tan(math.pi / n)
area = (n * s**2) / (4 * t)
print("Enter area of the polygon is %f" % area)

Testergebnisse:

 

"""
Demo 16
数据:一个四位整数number,求颠倒各位数字后的数
步骤:
1、提示用户输入一个四位整数
2、取个位。a = number % 10
3、去个位。number = number // 10
4、取十位。b = number % 10
5、去十位。number = number // 10
6、取百位。c = number % 10
7、去百位。number = number // 10
8、取千位。d = number % 10
9、输出颠倒后的数字
"""

number = int(input("Enter an integer:"))
a = number % 10
number = number // 10
b = number % 10
number = number // 10
c = number % 10
number = number // 10
d = number % 10
print("The reversed number is %d%d%d%d" %(a,b,c,d))

Testergebnisse:

 

"""
Demo 17
步骤:
1、提示用户输入十进制带小数的数字
2、元*100--->分 total
3、分 //100-->美元 a,分 % 100--->剩余分
4、剩余分 //25--->两角五分 b,再%25-->此时剩余分
5、4中剩余的分//10--->一角 c,再%10-->此时剩余分
6、5中剩余分//5--->五分 d,再%5-->此时剩余分
7、6中剩余分为一美分 e
"""
total = eval(input("Enter a number:"))
total *= 100
a = total // 100
total %= 100
b = total // 25
total %= 25
c =total // 10
total %= 10
d =total // 5
total %= 5
e = total
print("美元:%s   两角五分:%s  一角:%s  五分:%s  一美分:%s"%(a,b,c,d,e))
print(a * 100 + b * 25 + c * 10 + d * 5 + e)

Testergebnisse:

 

"""
Demo 18
数据:雇员姓名name,一周工作时间time,每小时报酬pay,联邦预扣税率rate1,州预扣税率rate2
步骤:
1、提示用户分别输入姓名,一周工作时间,每小时报酬,联邦预扣税率,州预扣税率
2、计算联邦扣税,州扣税,收到的总金额,扣税总金额
2、输出工资报表
"""
name = input("Enter employee's name:")
time = eval(input("Enter number of hours workedin a week:"))
pay = eval(input("Enter hourly pay rate:"))
rate1 = eval(input("Enter federal tax withholding rate:"))
rate2 = eval(input("Enter state tax withholding rate:"))

Pay = pay * time
FWithholding = Pay * rate1
SWithholding = Pay * rate2
TDeductipn = Pay * rate1 + Pay * rate2
NPay = Pay - Pay * rate1 - Pay * rate2
print("\nEmployee Name:%s" % name)
print("Hours Worked:%.1f" % time)
print("Pay Rate:$%.2f" % pay)
print("Gross Pay:%.1f" % Pay)
print("Deductipns:")
print("  Federal Withholding(%.1f):$%.2f" % (rate1,FWithholding))
print("  State Withholding(%.1f):$%.2f" % (rate2,SWithholding))
print("  Total Deduction:$%.2f" % TDeductipn)
print("Net Pay:$%.2f" % NPay)

Testergebnisse:
 

Ich denke du magst

Origin blog.csdn.net/trichloromethane/article/details/107967486
Empfohlen
Rangfolge