goods = {'apple': 5, 'banana': 3, 'orange': 4, 'watermelon': 10}balance = shopping_list = []while True: salary = input('请输入您的工资:') if salary == 'q': break try: salary = int(salary) except ValueError: print('请输入数字!') continue if salary <= : print('工资必须大于!') continue print('可购买的物品:') for name, price in goods.items(): print(name, price) choice = input('请选择要购买的物品(输入 q 退出):') if choice == 'q': break if choice not in goods: print('无效的选择!') continue price = goods[choice] if salary < price: print('工资不足!') continue balance += price shopping_list.append(choice) print('购买成功!') print('当前余额:', salary - price)print('购物清单:')for item in shopping_list: print(item)print('余额:', balance)帮我按照python的格式输出到成一个完整的py脚本
时间: 2023-03-29 14:01:53 浏览: 81
goods = {'apple': 5, 'banana': 3, 'orange': 4, 'watermelon': 10}
balance =
shopping_list = []
while True:
salary = input('请输入您的工资:')
if salary == 'q':
break
try:
salary = int(salary)
except ValueError:
print('请输入数字!')
continue
if salary <= :
print('工资必须大于!')
continue
print('可购买的物品:')
for name, price in goods.items():
print(name, price)
choice = input('请选择要购买的物品(输入 q 退出):')
if choice == 'q':
break
if choice not in goods:
print('无效的选择!')
continue
price = goods[choice]
if salary < price:
print('工资不足!')
continue
balance = price
shopping_list.append(choice)
print('购买成功!')
print('当前余额:', salary - price)
print('购物清单:')
for item in shopping_list:
print(item)
print('余额:', balance)
阅读全文