#购物车程序
gongzi = input("请输入您的工资:")
if gongzi.isdigit(): #判断变量是否为正整数,包括0
gongzi = int(gongzi)
else:
print("无效的工资,退出")
exit()
shangping = ['iphone','mac book','bike','book','pen']
buycars = []
print("
目前有以下商品:
")
for index,name in enumerate(shangping):
print(index+1,"-", name)
while True:
select = input("
请输入您想购买的商品名称,按q退出:
")
if select in shangping:
pass
elif select == 'q':
exit()
else:
print("对不起,您输入的商品不存在!请重新输入!
")
continue
if select == 'iphone':
if gongzi - 6800 >= 0:
gongzi = int(gongzi) - 6800
shangping.remove("iphone")
buycars.append("iphone")
print("
购买成功!已将您的商品放入购物车!
")
print("目前余额:",end="")
print(" 33[1;32m")
print(str(gongzi))
print(" 33[0m")
else:
print("
对不起,您的余额不足.
")
print("目前余额:",end="")
print(" 33[1;31m")
print(str(gongzi))
print(" 33[0m")
continue
elif select == 'mac book':
if gongzi - 12000 >= 0:
gongzi -= 12000
shangping.remove("mac book")
buycars.append("mac book")
print("
购买成功!已将您的商品放入购物车!
")
print("目前余额:",end="")
print(" 33[1;32m")
print(str(gongzi))
print(" 33[0m")
else:
print("余额不足.
")
print("目前余额:",end="")
print(" 33[1;31m")
print(str(gongzi))
print(" 33[0m")
continue
elif select == 'bike':
if gongzi - 700 >= 0:
shangping.remove("bike")
buycars.append("bike")
gongzi -= 700
print("
购买成功!已将您的商品放入购物车!
")
print("目前余额:",end="")
print(" 33[1;32m")
print(str(gongzi))
print(" 33[0m")
else:
print("余额不足.
")
print("目前余额:",end="")
print(" 33[1;31m")
print(str(gongzi))
print(" 33[0m")
elif select == 'book':
if gongzi -80 >= 0:
shangping.remove("book")
buycars.append("book")
gongzi -= 80
print("
购买成功!已将您的商品放入购物车!
")
print("目前余额:",end="")
print(" 33[1;32m")
print(str(gongzi))
print(" 33[0m")
else:
print("余额不足.
")
print("目前余额:",end="")
print(" 33[1;31m")
print(str(gongzi))
print(" 33[0m")
elif select == 'pen':
if gongzi - 50 >= 0:
shangping.remove("pen")
buycars.append("pen")
gongzi -= 50
print("
购买成功!已将您的商品放入购物车!
")
print("目前余额:",end="")
print(" 33[1;32m")
print(str(gongzi))
print(" 33[0m")
else:
print("余额不足.
")
print("目前余额:",end="")
print(" 33[1;31m")
print(str(gongzi))
print(" 33[0m")
print("购物车内的商品有:")
for buycar in buycars:
print("-",buycar)
print("共计:"+str(len(buycars))+"件商品")
print("余额:",gongzi,"
")