购物小程序改进版(只用了list)

#_author: FeiSir
#data: 2019/9/30

#利用列表嵌套

#购物列表

goods_list = [
('iPhone 11 Pro', 9999),
('iPhone 11', 6299),
('华为手环', 399),
('python book', 85),
('cup ', 59),
('pen', 5)
]
salary_of_shopper = input("Please input your salary:")
if salary_of_shopper.isdigit():
money_shopper = int(salary_of_shopper)
flag = True
buyed_list = []
while flag:
print("可购物列表为:")
# for i in range(len(goods)):
# print("%d、%s %d"%(i+1,goods[i][0],goods[i][1]))
for i,j in enumerate(goods_list,1):
print(i,":",j)
index_choice = input('Please input the index of goods you want(or quit:Q):')
if index_choice.isdigit():
index_choice=int(index_choice)
if index_choice > 0 and index_choice< len(goods_list):
if goods_list[index_choice][1] > money_shopper:
print("余额不足,¥%d" % (money_shopper - goods_list[index_choice][1]))
# flag_continue = input('是否继续购物(Y/N)')
# if flag_continue == "N":
# flag = False
else:
buyed_list.append(goods_list[index_choice-1])
money_shopper -= goods_list[index_choice-1][1]
print("已将 %s加入您的购物车,余额为¥%d" % (goods_list[index_choice-1][0], money_shopper))
# flag_continue = input('是否继续购物(输入N退出,其余任意键继续购物)')
# if flag_continue == "N":
# flag = False
else:
print("编号不存在")
elif index_choice == "Q":
the_number_buyed = len(buyed_list)
print("-----您购买的商品为:----------")
# print(buyed_list)
for i, j in enumerate(buyed_list, 1):
print(i, ":", j)
print("""
-----您的余额为:%d,欢迎下次购买!
""" % money_shopper)
break
else:
print("非法输入")
else:
print("非法输入")

猜你喜欢

转载自www.cnblogs.com/ofhanfei/p/11613398.html