第一周练习笔记

完整代码

```salary = input('input your salary:')
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input('choice number:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_itam = product_list[user_choice]
if p_itam[1] <= salary:
shopping_list.append(p_itam)
salary -= p_itam[1]
print('Goods %s has been added to the shopping cart, the balance is %s'%(p_itam,salary))
else:
print('Sorry, the balance is not enough.balace is %s'%(salary))
else:
print('Please enter the correct serial number')
elif user_choice == 'q':
print('----------shopping list-------------')
for p in shopping_list:
print(p)
print('your blance is %s \n --------END--------'%(salary))
exit(print('Welcome to this system\n----Power By Erro----'))

else:

代码解析及重点

• 购物清单解析 使用列表实现购物的清单，嵌入列表实现物品与价格的对应。
```product_list = [    #列表实现购物清单和价格对应
('mac_pro',9800),
('iphone',6800),
('bike',800),
('watch',9000),
('coffee',60),
('book',120),
]
shopping_list = [] #建立购物车列表实现购物清单的显示```

• 实现用户交互，输入获取
`salary = input('input your salary:')`

• 输入判断 检测输入字符串是否只由数字组成
`salary.isdigit()`

• 列表输出序列号
```for index,item in enumerate(product_list):
print(index,item)```

21 篇文章26 人订阅

0 条评论

相关文章

1814

3394

1.1K5

3403

1143

3765

2381

Web应用程序开发指南

Web应用程序是与服务器端编程相结合的动态Web站点，它提供诸如与用户交互，连接到后端数据库以及向浏览器生成结果等功能。

1762

4097