第二周练习 part 1

python学习网 2018-03-13 09:13:02
#购物车程序

'''启动程序后让用户输入工资,然后打印商品列表
用户根据编号选择购买的商品
用户的钱足够就直接扣钱,不够就提醒
可随时退出,退出时打印已购买商品和余额'''

salary=input("请输入你本月的工资: ")
goods=['1.可乐(12)','2.雪碧(13)','3.Notebook(5)','4.iPhone(8888)','5.Earphone(3643)']
price=[12, 13, 5, 8888, 3643]
cost=0
bought = []
startshop = 1
k = 0
print(goods)
while startshop == 1:
i = input('Please type the goods number of what you want to buy, when you are done, type "0" to quit: ')
if int(i) == 0:
startshop = 0
else:
if price[int(i) - 1] + cost > int(salary):
print("You don't have enough money.")
j = input('Do you want to buy other goods? Type 1 for yes, type 0 for quit: ')
if int(j) == 1:
continue
if int(j) == 0:
startshop = 0
else:
cost = cost + price[int(i) - 1]
bought.insert(k,goods[int(i)-1])
k+= 1
print("You have bought ", bought[:], " and cost ", cost, " yuan.", "Now you have", int(salary) - cost, "yuan left")

-----------------------------------------------------New Version-------------------------------------------------------

salary=input("Please input your salary: ")
if salary.isdigit():
salary = int(salary)
goods=['Coco(12 yuan)','Sprite(13 yuan)','Notebook(5 yuan)','iPhone(8888 yuan)','Earphone(3643 yuan)']
price=[12, 13, 5, 8888, 3643]
cost=0
bought = []
startshop = 1
k = 0
for a in enumerate(goods): print(a)
while True:
i = input('Please type the goods number of what you want to buy, when you are done, type "q" to quit>>> ')
if i.isdigit():
i = int(i)
if i > 4 or i < 0:
print('Pleasure type the number shown before the merchandise.')
continue
if price[i] + cost > int(salary):
print("You don't have enough money.")
while True:
j = input('Do you want to buy other goods? Type y for yes, type q for quit: ')
if j == 'y':
break
elif j == 'q':
print("You have bought")
for w in bought:
print(w)
print("and cost", cost, "yuan.", "Now you have", salary - cost, "yuan left.")
exit()
else:
print('\033[41;1mPleasure type "y" or "q"!\033[0m')
continue
else:
cost = cost + price[i]
bought.insert(k, goods[i])
k += 1
print('Added', goods[i], 'to your list successfully. Now you have', salary - cost, 'yuan left.')
elif i == 'q':
print("You have bought")
for w in bought:
print(w)
print("and cost", cost, "yuan.")
print("Now you have", salary - cost, "yuan left.")
exit()
阅读(769) 评论(0)