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)将行数标出来
时间: 2023-03-29 09:01:55 浏览: 67
1. goods = {'apple': 5, 'banana': 3, 'orange': 4, 'watermelon': 10}
2. balance = shopping_list = []
3. while True:
4. salary = input('请输入您的工资:')
5. if salary == 'q':
6. break
7. try:
8. salary = int(salary)
9. except ValueError:
10. print('请输入数字!')
11. continue
12. if salary <= :
13. print('工资必须大于!')
14. continue
15. print('可购买的物品:')
16. for name, price in goods.items():
17. print(name, price)
18. choice = input('请选择要购买的物品(输入 q 退出):')
19. if choice == 'q':
20. break
21. if choice not in goods:
22. print('无效的选择!')
23. continue
24. price = goods[choice]
25. if salary < price:
26. print('工资不足!')
27. continue
28. balance = price
29. shopping_list.append(choice)
30. print('购买成功!')
31. print('当前余额:', salary - price)
32. print('购物清单:')
33. for item in shopping_list:
34. print(item)
35. print('余额:', balance)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)