2023.10.31

发布时间 2023-10-31 18:13:02作者: 我要变富婆

运行超市抹零结账行为

代码如下:

1 print("3107")
2 money = 39.87 + 24.47 + 78.07       #计算总金额
3 money_str = str(money)
4 print("商品总金额:" + money_str)
5 print("实收金额:{:.0f}".format(money))  #进行抹零行为

结果如下:

计算学生成绩的分差和平均分

代码如下:

复制代码
1 print("3107")
2 Python = 95
3 C = 76
4 math = 85
5 cha = Python - C
6 avg = (Python + C + math)/3
7 print("Python与C语言相差{}分。".format(cha))
8 print("三科平均分为{:.2f}分。".format(avg))
复制代码

结果如下:

比较运算符比较大小关系:

代码如下:

复制代码
 1 print("3107")
 2 Python = 95
 3 C = 76
 4 math = 85
 5 print("Python = 95 C = 76 math = 85")
 6 print("Python > C 的比较结果:" + str(Python > C))
 7 print("Python < C 的比较结果:" + str(Python < C))
 8 print("Python == math 的比较结果:" + str(Python == math))
 9 print("Python != math 的比较结果:" + str(Python != math))
10 print("math >= C 的比较结果:" + str(math >= C))
11 print("math <= C 的比较结果:" + str(math <= C))
复制代码

结果如下:

手机店打折活动

代码如下:

复制代码
1 print("3107")
2 print("手机店打折活动正在进行中~")
3 week = input("请输入今天星期(如:星期一):")
4 time = int(input("请输入当前整点时间(0~23):"))
5 if (week == "星期二" and (time >= 10 and time <= 15) ) or  (week == "星期五" and (time >= 16 and time <= 20) ):
6     print("恭喜中奖啦!快来参与打折活动吧!")
7 else:
8     print("很遗憾没有中奖,请期待下次活动。")
复制代码

结果如下:

 

新建文件