python学习(编程题基本语法篇1~18题)

Demo 01

在这里插入图片描述

"""
数据 : 华氏温度 摄氏温度
指令:
1、提示用户输入摄氏温度
2、利用已知公式将摄氏温度转换成华氏温度
3、输出两者的值
"""
celsius = eval(input("Enter a degree in Celsius:"))
fahrenheit = (9/5) * celsius + 32
print("Celsius is %0.1f Fahrenheit"%(fahrenheit))

#法二(老师写的)
cel = float(input("Enter a degree in Celsius:"))
fah = (9 / 5) * cel + 32
# .0 保留0位小数        .1 保留1位小数
print("%.0f Celsius is %.1f Fahrenheit" % (cel, fah))

运行结果
在这里插入图片描述

Demo02

在这里插入图片描述

"""
数据:圆柱半径 圆柱高
指令:
1、提示用户输入半径和高
2、计算圆柱体底面积
3、计算圆柱体高
"""

r,l = eval (input ("Enter the radius and length of cylinder:"))
area = r * r * 3.1415
volume = area * l
print("The area is %f \n The volume is %f"%(area,volume))

在这里插入图片描述

Demo03

在这里插入图片描述

"""
数据:0-1000之间的整数
指令:
1、提取百位、十位、个位上的数字
2、将提取出来的数字相加
"""
num = eval(input ("Enter a number between 0 and 1000:"))
num1 = num % 10
num2 =  ( num % 100 ) // 10
num3 = num // 100
num = num1 + num2 + num3
print("The sum of digits is %.f"%(num))

# 法2 老师写的
"""
数据:输入一个三位数 个位 十位 百位
步骤:
1.输入一个不超过三位的数字num=123
2.num对10取余 得到 个位 3 - num1
3.num整除10 12
4.num对10取余 得到 十位 2 - num2
5.num整除10 1
6.num对10取余 得到 百位 1 - num3
7.将num1+num2+num3
"""
number = int(input("Enter a number between 0 and 1000:"))
number = abs(number)
num1 = number % 10
number = number // 10
num2 = number % 10
number = number // 10
num3 = number % 10
total = num1 + num2 + num3
# print("The sum of the digits is " + str(total))
print("The sum of the digits is ", total)
该方法适合用于递归调用里,可扩展性较好

在这里插入图片描述

Demo04

在这里插入图片描述

"""
数据:分钟数转变成年数和天数
指令:
一天=60*24
一年=365天=365*60*24
1、提示用户输入分钟数
2、计算出年数
3、余下分钟计算出天数
"""
num = eval(input("Enter the number of minutes:"))
num1 = num
year = num // (365 * 60 * 24)
num = num % (365 * 60 * 24)
day =  num // (60 * 24)
print ("%.f minutes is approximately %.f years and %.f days"%(num1,year,day))

在这里插入图片描述

Demo05

在这里插入图片描述

"""
数据:初始温度 最终温度 所需能量
指令:
1、提示用户输入输入水的重量、初始温度、最终温度
2、编写公式
3、输出结果
"""
M = eval(input("Enter the amount of water i kilograms:"))
initial = eval (input ("Enter the initial temperature:"))
final = eval (input ("Enter the final temperature:"))
Q= M * (final - initial) * 4184
print("The energy needed is %.01f"%Q)

在这里插入图片描述

Demo06

在这里插入图片描述

"""
数据:华氏度 风速
指令:
1、提示用户输入华氏度和风速
2、带入公式计算
3、输出结果
"""
Ta = eval(input("Enter the temperature in  Fahrenheit between -58 and 41:"))
v = eval(input ("Enter the speed in miles per hour :"))
Twc = 35.74 + 0.6215 * Ta - 35.75 * ( v ** 0.16 ) + 0.4275 * Ta * ( v ** 0.16 )
print("The wind chill index is %f"%Twc)

在这里插入图片描述

Demo07

在这里插入图片描述

"""
数据:飞机加速度  起飞速度
指令:
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 %0.003f"%(length))

在这里插入图片描述

Demo08

在这里插入图片描述

"""
数据: 四位整数
指令:
1、提示用户输入四位整数
2、将每一位上的数剥离另外存起来
3、按反向顺序输出
"""
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 ("%d\n%d\n%d\n%d\n"%(num1 , num2 , num3, num4 ))

在这里插入图片描述

Demo09

在这里插入图片描述

"""
数据:三角形的三个顶点(x1,y1)\(x2,y2)\(x3,y3)
指令:
1、输入三个点坐标
2、计算三边距离
3、运行海伦-秦九韶公式
4、输出结果
"""

# 法2 老师写的
import math
x1,y1,x2,y2,x3,y3 = eval(input("Enter three points for a triangle:"))
side1 = math.sqrt((x1- x2) *(x1 - x2)+(y1- y2)*(y1 - y2))
side2 = math.sqrt((x2- x3) *(x2 - x3)+(y2- y3)*(y2 - y3))
side3 = math.sqrt((x1- x3) *(x1 - x3)+(y1- y3)*(y1 - y3))
s = (side1 + side2 + side3) / 2
area = math.sqrt (s * ( s - side1 ) * ( s - side2) * ( s - side3 ))
print("The area of triangle is %0.01f"%(area))

"""
数据:三个点的坐标 x1y1 x2y2 x3y3
步骤:
1.提示用输入三个点坐标
2.两两计算边长
3.计算半周长s
4.计算面积
"""
x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle:"))
side1 = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
side2 = ((x3 - x1) ** 2 + (y3 - y1) ** 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)

在这里插入图片描述

Demo10

在这里插入图片描述

"""
数据:正六边形边长
指令:
1、提示用户输入正六边形边长
2、罗列公式并代入
3、输出结果
"""
import math
side = eval(input ("Enter the side :"))
area = ((3 * math.sqrt(3) ) / 2 ) * math.pow(side,2)
print("The area of the hexagon is %f"%area)

# 法二 老师写的
"""
数据:边长 面积
步骤:
1.提示用户输入边长
2.利用公式计算面积
3.输出面积
"""
side = float(input("Enter the side:"))
area = 3 * 3 ** 0.5 / 2 * side ** 2
print("The area of the hexagon is",area)

在这里插入图片描述

Demo11

在这里插入图片描述

"""
数据:当前从历元到现在的时间(总秒数)  秒 分钟 小时
指令:
1、将历元时间获得的秒数取整
2、计算当前的秒数
3、计算总分钟数
4、计算当前的分钟数
5、计算总小时
6、计算总小时数
"""
import time
qushi = eval(input ("Enter the time zone offset to GMT:"))
ct = time.time()
total_seconds = int(ct)
cur_seconds = total_seconds % 60
total_minutes  = total_seconds // 60
cur_minutes  = total_minutes % 60
total_hours = total_seconds // (60 * 60)
cur_hours = ( total_hours % 24 + qushi ) % 24
print(cur_hours,":",cur_minutes ,":",cur_seconds)

# 法二,老师写的
"""
import time
time.time()
1596595829.7057564 s
以秒为单位以浮点数返回历元之后的时间
数据:秒 分钟 小时 总秒数
步骤:
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 offset 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(cur_hours, ":", cur_minutes, ":", cur_seconds)

在这里插入图片描述

Demo12

在这里插入图片描述

"""
根据用户输入的每月存款数得出六个月后,账户总额
数据:每个月存款数a
步骤:
1.提示用户输入每月存款数a;
2.第一个月后m=a(1+0.00417);
3.第二个月后m=(a+m)(1+0.00417);
4.第三个月后m=(a+m)(1+0.00417);
5.第四个月后m=(a+m)(1+0.00417);
6.第五个月后m=(a+m)(1+0.00417);
7.第六个月后m=(a+m)(1+0.00417);
3.输出六个月后账户总和。
"""
a = float(input("Enter the monthly saving amount:"))
m = a * (1 + 0.00417)
m = (a + m) * (1 + 0.00417)
m = (a + m) * (1 + 0.00417)
m = (a + m) * (1 + 0.00417)
m = (a + m) * (1 + 0.00417)
m = (a + m) * (1 + 0.00417)
print ("After the sixth month ,the account value is %.2f"%(m))


# 法二 第二次写的为了更好把它改的实用一点写到了12个月
"""
数据:每月存款 年利率5%  月利率0.0.5/12=0.000417   账户总额
指令:
1、提示用户输入每月存款
1、第一个月*(1 + 0.00417 )
2、第二个月本金加利息再*(1 + 0.0.00417)
……
3、输出结果
"""
bj = eval(input ("Enter the monthly saving amount :"))
bj1 = bj * (1 + 0.00417)
bj2 = (bj + bj1) * (1 + 0.00417)
bj3 = (bj + bj2) * (1 + 0.00417)
bj4 = (bj + bj3) * (1 + 0.00417)
bj5 = (bj + bj4) * (1 + 0.00417)
bj6 = (bj + bj5) * (1 + 0.00417)
bj7 = (bj + bj6) * (1 + 0.00417)
bj8 = (bj + bj7) * (1 + 0.00417)
bj9 = (bj + bj8) * (1 + 0.00417)
bj10 = (bj + bj9) * (1 + 0.00417)
bj11 = (bj + bj10) * (1 + 0.00417)
bj12 = (bj + bj11 )* (1 + 0.00417)
print("After the sixth month,the account value is %f"%bj6)

Demo13

在这里插入图片描述

"""
读取投资额、年利率和年数得出未来投资额
数据:投资额a   年利率b 年数c
步骤:
1.提示用户输入投资额a、年利率b、年数c;
2.计算月投资率d=b/12;
3.计算月数e=c*12;
4.根据公式计算m=a*(1+d)**e;
5.输出结果m.
"""
a = float (input ("Enter investment amount :"))
b = float (input("Enter annual interest rate :"))
c = float (input ("Enter number of years:"))
d = b /100/ 12
e = c * 12
m = a * ((1 + d) ** e)
print ("Accumulated value is %.2f"%(m))



Demo14

在这里插入图片描述

"""
数据: 三角形三条边
指令:
1、提示用户输入三条边
2、用公式计算d得到弧度再转角度
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 - b * b - a * a ) / ( -2 * a * b ))
A = math.degrees(A)
B = math.degrees(B)
C = math.degrees(C)
print("三边对应的的三个角为: %.001f %.001f %.001f"%(A,B,C))

# 法二 老师讲的
import math

a, b, c = eval(input("Enter three sides of a triangle:"))
A = math.acos((a * a - b * b - c * c) / (-2 * b * c))
B = math.acos((b * b - c * c - a * a) / (-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))

在这里插入图片描述

Demo15

在这里插入图片描述

"""
数据:边长s 边数n
步骤:
1.提示用户输入边数n和边长s;
2.列出公式Area = (n * s * s) / (4 * tan (math.pi / n);
3.输出结果。
"""
import  math
n = int (input ("Enter the numer of sides :"))
s = float (input ("Enter the side :"))
Area = (n * s * s) / (4 * math.tan(math.pi / n))
print("The area of the polygon is ",Area)

# 第二次写的
"""
数据:边数  边长 面积
指令:
1、提示用户输入正多边形的边数和边长
2、代入计算公式
3、输出结果
"""
import math
side = eval(input("Enter the number of sides:"))
length = eval(input ("Enter the side:"))
area = (side *  (length ** 2 )) / (4 * math.tan(math.pi / side))
print("The area of the polygon is %f "%area)

在这里插入图片描述

Demo16

在这里插入图片描述

"""
颠倒四位整数各位数字
数据:四位整数
步骤:
1.提示用户输入四位整数number;
2.number对10取余,得到个位上的数num1;
3.number整除10;
4.number对10取余,得到十位上的数num2;
5.number整除10;
6.number对10取余,得到百位上的数num3;
7.number整除10;
8.number对10取余,得到千位上的数num4;
9.total=num1*1000+num2*100+num3*10+num4;
10.输出结果。
"""
number = int (input("Enter an integer:"))
num1 = number % 10
number = number // 10
num2 = number % 10
number = number //10
num3 = number % 10
number = number //10
num4 = number % 10
total = num1 * 1000 + num2 * 100 + num3 * 10 + num4
print ("The reversed number is ",total)

在这里插入图片描述

Demo17

在这里插入图片描述

"""老师写的
数据:美元a 二角五分b 一角c 五分d 一分e
步骤:
1.提示用户输入美元值(最多两个小数点)
2.将这个值换算成分total
3.total // 100 美元硬币个数 a
4.total % 100 剩下的分 -> total
5.total // 25 二角五分硬币个数 b
6.total % 25 剩下的分 -> total
7.total // 10 一角硬币个数 c
8.total % 10 剩下的分 -> total
9.total // 5 五分硬币个数 d
10.剩下就是e
"""
total = float(input("Enter dollas:"))
total = int(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(a, b, c, d, e)
print(a * 100 + b * 25 + c * 10 + d * 5 + e * 1)

#法二 自己写的
"""
数据:总金额 美元 两角五分硬币 一角硬币 五分硬币 美分
指令:
1、提示用户输入十进制带小数点的数字
2、将钱转换成分数
3、将分数除以100得到美元个数,使用分数取余得到余数即剩余的分数
4、将剩余的分数除以25得到两角五分的硬币数,取余的剩余分数
5、剩余分数除10得一角硬币数,取余得剩余分数
6、剩余分数除以5得五分硬币数,取余得剩余分数
7、显示结果
"""
num = eval(input ("输入美元数:"))
total = int (num * 100)
my  = total // 100
total = total % 100
mf1 = total // 25
total = total % 25
mf2 = total // 10
total = total % 10
mf3 = total // 5
total = total % 5
print(my,mf1,mf2,mf3,total )
print( my * 100 + mf1 * 25 + mf2 * 10 + mf3 * 5 + total *1)

在这里插入图片描述

Demo18

在这里插入图片描述

"""
根据用户提供的信息打印工资报表
数据:姓名Name  一周工作时间Time 每小时报酬m 联邦预扣税率r 州预扣税率a
步骤:
1.提示用户输入信息姓名Name 、一周工作时间Time、每小时报酬m、联邦预扣税率r、州预扣税率a;
2.计算一周总收入G=Time*m;
3.计算联邦税费R;
4.计算周税费A;
5.计算总税费D;
6.计算最终收入M.
7.输出工工资报表。
"""
Name = str (input ("Enter employee's name :"))
Time = float (input ("Enter number of hours worked in a week :"))
m = float (input ("Enter hourly pay rate :"))
r = float (input ("Enter federal tax withholding rate :"))
a = float (input ("Enter state tax withholding rate :"))
G = Time * m
R = G * r
A = G * a
D = R + A
M = G - R - A
print ("Employee Name :",Name)
print ("Hours Worked:",Time)
print ("Pay Rate: $%.2f"%(m))
print ("Gross Pay :$%.2f"%(G))
print ("Deductions:")
print ("Federal Withholding (%s):$%.2f"%(r,R))
print ("State Withholding (%s):$%.2f"%(a,A))
print ("Total Deduction :$%.2f"%(D))
print ("Net Pay :$%.2f"%(M))

在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值