要求:顾客可以根据商品编号购买工资允许内的商品,购买完成后,打印购买列表。
def main():
salary = input("what is your salary ?")
if salary.isdigit():
salary = int(salary)
else:
print("请输入数字!")
shopping_list = [("iphone",5000),
("notebook",8000),
("coffee",30),
("book",50),
("bike",500)
]
print(shopping_list)
shopping_cart = []
for i in shopping_list:
print("商品编号:",shopping_list.index(i),"价格为:",shopping_list[shopping_list.index(i)][1])
question = input("do you want it ?(yes/no)")
if question == "yes" :
if shopping_list[shopping_list.index(i)][1] > salary:
print("余额不足!")
else:
print("购买成功!")
salary = salary - shopping_list[shopping_list.index(i)][1]
shopping_cart.append(shopping_list[shopping_list.index(i)])
elif question == "no":
question2 = input("do you want to exit?(yes/no)")
if question2 == "yes":
break
else:
continue
else:
print("输入错误")
print("you have bought:",shopping_cart) if __name__ == "__main__":
main()