Python购物车之用户部分代码
来源:动视网
责编:小采
时间:2020-11-27 14:24:48
Python购物车之用户部分代码
Python购物车之用户部分代码:知识点: 文件读,写操作,if 判断, for 循环salary = input("输入你的工资:") bought_list = [] product_list = {} with open("product_list","r",encoding="utf-8&q
导读Python购物车之用户部分代码:知识点: 文件读,写操作,if 判断, for 循环salary = input("输入你的工资:") bought_list = [] product_list = {} with open("product_list","r",encoding="utf-8&q

知识点: 文件读,写操作,if 判断, for 循环
salary = input("输入你的工资:")
bought_list = []
product_list = {}
with open("product_list","r",encoding="utf-8") as f1:
for item in f1:
p_name,p_price = item.strip().split(':')
product_list[p_name]=int(p_price)
print(product_list)
if salary.isdigit():
salary = int(salary)
while True:
user_choice = input("please input product which you want:")
if user_choice in product_list.keys():
if product_list[user_choice] <= salary:
bought_list.append(user_choice) #增加列表的元素
salary = salary - product_list[user_choice]
print("Had bought [