1.编写一个买菜计价的程序,该程序主要功能可以显示菜单,通过输入菜号来选择买什么菜,之后程序会报出你买了多少个菜,用了多少钱。请用所学知识设计出此程序。 2.随机输入10名同学的成绩,统计出10名同学的优秀率和及格率。
时间: 2024-05-29 14:10:00 浏览: 23
1.买菜计价程序:
# 定义菜单字典
menu = {
'1': {'name': '青菜', 'price': 2},
'2': {'name': '土豆', 'price': 3},
'3': {'name': '西红柿', 'price': 4},
'4': {'name': '黄瓜', 'price': 2.5},
'5': {'name': '茄子', 'price': 3.5}
}
# 显示菜单
print('菜单如下:')
for key, value in menu.items():
print(key, value['name'], value['price'])
# 购买菜品
total_num = 0 # 总购买数量
total_price = 0 # 总花费金额
while True:
choice = input('请输入要购买的菜品编号(按q退出):')
if choice == 'q':
break
if choice not in menu:
print('输入有误,请重新输入!')
continue
num = input('请输入购买数量:')
try:
num = int(num)
if num <= 0:
print('输入有误,请重新输入!')
continue
except:
print('输入有误,请重新输入!')
continue
total_num += num
total_price += num * menu[choice]['price']
print('已购买{}个{},花费{}元。'.format(num, menu[choice]['name'], num * menu[choice]['price']))
# 显示购买结果
print('您购买了{}个菜品,总共花费了{}元。'.format(total_num, total_price))
2.成绩统计程序:
import random
# 随机生成10名同学的成绩
scores = []
for i in range(10):
scores.append(random.randint(0, 100))
print('10名同学的成绩为:', scores)
# 统计优秀率和及格率
excellent_count = 0 # 优秀人数
pass_count = 0 # 及格人数
for score in scores:
if score >= 90:
excellent_count += 1
if score >= 60:
pass_count += 1
excellent_rate = excellent_count / len(scores)
pass_rate = pass_count / len(scores)
print('优秀率为:{:.1%},及格率为:{:.1%}'.format(excellent_rate, pass_rate))