利用列表的增删改查及while,for 循环和if语句编写购物程序
1.粗制滥造:(此程序多处写死)
money = int(input("please input money what you have:"))
goods = [
'1.apple ', '20',
'2.bbq ', '30',
'3.cool ', '40',
'4.duck ', '10']
items = []
countM = 0
while True:
print(goods)
item = input("Please select your item(input the item's num;input q exit): ")
if item == "1":
items.append(goods[0:2])
countM += int(goods[1])
print(goods[0:2], "have have been put into the shopping cart")
elif item == "2":
items.append(goods[2:4])
countM += int( goods[3])
print(goods[2:4], "have have been put into the shopping cart")
elif item == "3":
items.append(goods[4:6])
countM += int( goods[5])
print(goods[4:6], "have have been put into the shopping cart")
elif item == "4":
items.append(goods[6:8])
countM += int( goods[7])
print(goods[6:8], "have have been put into the shopping cart")
elif item == "q":
print( "shopping cart list:",items)
balance = money - countM
if balance >= 0:
print("thanks; your balance is",balance)
else:
print("your balance is :",balance,"not ennugh!!!")
break
else:
print("please input the item's num")
continue
2精品程序(动态程序)
money = input("please input money what you have:")
countM = 0
goods = [
('apple', 20),
('bbq ', 30),
('cool ', 40),
('duck ', 10),
]
items = []
if money.isdigit():
money = int(money)
while True:
for index, b in enumerate(goods):
print(index+1,b)
item = input("Please select your item(input the item's num;input q exit): ")
if item.isdigit():
item=int(item)
if item < len(goods) and item > 0:
items.append(goods[item-1])
countM += goods[item-1][1]
print("your shopping cart list:",items)
else:
print("item [%s] is not exist"% item)
elif item == "q":
##print("shopping cart list:"
balance = money - countM
if balance >= 0:
info = '''
--------thanks info-------
items:{0}
spend: {1}
balance: {2}
'''.format(items,countM,balance)
print(info)
else:
print("your balance is :",balance,"not ennugh!!!")
break
else:
print("please input the item's num")
continue