Python简单的购物车程序
#Author:xuhan # -*- coding:utf-8 -*-shopping_car = [] while True:
salary = input('请输入您的购物预算:')
if salary.isdigit():
break
else
: continue
print(' 欢 迎 光 临 家 乐 福 购 物 中 心 '.center(60,'-'))
product_list = [('Iphone7',5999),('Mac Are',6999),('UNIQL',200)]
choice_flag = ''
while choice_flag != 'q': #判断用户选择购物还是离开
for product_item in range(len(product_list)):
print(product_item,product_list,':',product_list) #打印商品信息
choice=input('请输入您想购买的产品序号[或输q键结束购物]:')
if choice.isdigit() :
if int(choice) < len(product_list): #判断所选择的产品是否存在
if int(salary)>=int(product_list):#判断余额是否足够支付选择的产品
shopping_car.append(product_list) #如果足够就添加到购物车
salary=int(salary)-int(product_list) #从余额扣除所选商品的价格
print('已购买商品:',shopping_car) #打印购物车内产品
else:
print('对不起,您的余额不足,剩余:',salary,'元!') #余额不足进行提醒
continue
else:
print('对不起,您选择的商品暂时不存在!') #如果不存在进行提醒
elif choice !='q':#选择结束购物
continue
else:
print('*'*21)
print(' 再见,欢迎下次光临! ')
print('*' * 21)
break
页:
[1]