Day2:购物车小程序

一、购物车小程序第一版

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
product_list = [
("iphone 8", 5888),
("Mi Note3", 2499),
("Bike", 788),
("Phthon 3.5", 59),
("Mac pro", 9888),
("Honor 6", 599)
]
notfalse = not False
shopping_cart = []
while notfalse:
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary) while True:
# for i in product_list:
# print(product_list.index(i),i)
for index, i in enumerate(product_list):
print(index, i) # 打印商品列表
user_choice = input("Please choose what you need to buy:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0: # 用户选择了对应的商品
if product_list[user_choice][1] <= salary: # 买得起
salary = salary - product_list[user_choice][1] # 扣钱
shopping_cart.append(product_list[user_choice])
print("You have purchased %s, and your current balance is %s." % (
product_list[user_choice], salary))
elif user_choice == "": # 直接按的回车,没有输入
print("Please enter the corresponding number of the goods!")
else: # 钱不够,买不起!
print("Your balance is not enough!")
else: # 用户输入的数字超出商品总数
print("Please check the commodity number!")
elif user_choice == "q" or user_choice == "Q": # 要退出
print("------------shopping list-------------")
for index, i in enumerate(shopping_cart):
print(index, i)
print("Your current balance is %s." % salary)
exit()
else: # 用户输入的不是数字
print("Please enter the corresponding number of the goods!") elif salary == "q" or salary == "Q": # 用户想退出
notfalse = False
else: # 用户输入了其他字符
print("Please input your salary by number!")

  

上一篇:stingray后端开发


下一篇:python 购物车小程序