1、一家商场在降价促销,所有原价都是整数(不需要考虑浮点情况),如果购买金额50-100元 (包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣。编写一程序, 询问购买价格,再显示出折扣(%10或20%)和最终价格。
price = eval(input('请输入价格:'))
if 50 <= price <= 100:
print('当前折扣为10%')
new_price = price * 0.9
print('折扣后价格为:{}'.format(new_price))
elif price > 100:
print('当前折扣为20%')
new_price = price * 0.8
print('折扣后价格为:{}'.format(new_price))
else:
print('当前价格无折扣')
打印结果:
请输入价格:49
当前价格无折扣
请输入价格:50
当前折扣为10%
折扣后价格为:45.0
请输入价格:55
当前折扣为10%
折扣后价格为:49.5
请输入价格:99
当前折扣为10%
折扣后价格为:89.10000000000001
请输入价格:101
当前折扣为20%
折扣后价格为:80.80000000000001
2 判断是否为闰年 提示: 输入一个有效的年份(如:2019),判断是否为闰年(不需要考虑非数字的情况) 如果是闰年,则打印“2019年是闰年”;否则打印“2019年不是闰年” 什么是闰年,请自行了解(需求文档没有解释)
year = int(input('请输入一个年份:')) # 输入一个整数的年份
if (year % 100 != 0 and year % 4 == 0) or (year % 100 == 0 and year % 400 == 0):
print('你输入的' + str(year) + '是闰年')
else:
print('你输入的' + str(year) + '不是闰年')
打印结果:
请输入一个年份:2024
你输入的2024是闰年
3.求三个整数中的最大值 提示:定义 3 个变量
a = 1
b = 9
c = 7
#方式1:
if a > b:
if a > c:
print(a)
else:
print(c)
else:
if b > c:
print(b)
else:
print(c)
#方式2:
new_1 = 10
new_2 = 20
new_3 =30
print("使用mac函数",max(new_1,new_2,new_3))
打印结果:
方式1最大值:9
方式2最大值:30
进阶版作业(选 做题 ,可不做哈): 4、编写如下程序 a.用户输入1-7七个数字,分别代表周一到周日 b.如果输入1~5,打印对应的“周一”~“周五”,如果输入的数字是6或7,打印输出“周末” c.如果输入0,退出循环 d.输入其他内容,提示:“输入有误,请重新输入!” 提示:本题可以使用if和while循环,同时需要校验用户的输入是否正确。不用考虑浮点数等情况。
while True:
weekend =int(input("请输入数字:"))
dict = {1: "周一", 2: "周二", 3: "周三", 4: "周四", 5: "周五", }
if 1<= weekend <=5:
print(f"今天日期是{dict[weekend]}")
elif 6<= weekend <=7:
print("今天日期是周末")
elif weekend == 0:
break
else:
print("你输入的有误,请重新输入")
打印结果:
请输入数字:1
今天日期是周一
请输入数字:2
今天日期是周二
请输入数字:3
今天日期是周三
请输入数字:4
今天日期是周四
请输入数字:5
今天日期是周五
请输入数字:6
今天日期是周末
请输入数字:7
今天日期是周末
请输入数字:8
你输入的有误,请重新输入
请输入数字:0