python实现简单的购物程序
需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额
#!/usr/bin/ven python # Author: Hawkeye ‘‘‘ 本程序为实例程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ‘‘‘ #创建商品列表 product_list = [ ["Iphone",5800], ["Mac Pro",9800], ["bike",800], ["watch",10600], ["coffee",31], ["Alex Python",20] ] # for i in product_list: # print(i) #创建购物列表 shopping_list =[] #要求用户输入数据 salary = input("Input your salary:") #首先要对用户的输入做判断 if salary.isdigit(): salary = int(salary) #转换为整形 while True: #循环输出列表 for index,item in enumerate(product_list): print(index,item) user_choice = input("请选择要买什么......") if user_choice.isdigit():#转换为整形 user_choice =int(user_choice) if user_choice < len(product_list) and user_choice >=0: p_item = product_list[user_choice] if p_item[1] <=salary:#钱够 shopping_list.append(p_item) salary -= p_item[1] print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % (p_item,salary) ) else:#钱不够 print("\033[41;1m您的余额只剩【%s】,余额不足\033[0m" %salary) else: print("\033[32;1mProduct code [%s]is not exist\033[0m " %user_choice) elif user_choice == "q": print("----------shoppig list--------") for p in shopping_list: print(p) print("------------------------------") print("\033[33;1mYour current balance is :\033[0m",salary) exit() else: print("Invalid Option") else:#输入q退出 print("\033[13;1m【错误】请输入正确的数字!\033[0m") exit()
相关推荐
elizabethxxy 2020-11-06
pythonxuexi 2020-10-30
retacnyue 2020-09-28
pythonxuexi 2020-09-06
Morelia 2020-09-04
zhaobig 2020-08-17
linkequa 2020-08-16
CloudXli 2020-08-14
kikaylee 2020-08-12
LowisLucifer 2020-08-09
xiesheng 2020-08-06
Tristahong 2020-08-05
CatherineC00 2020-08-01
Andrewjdw 2020-07-26
reallyr 2020-07-18
wordmhg 2020-07-16
yawei 2020-07-06
zlfing 2020-07-07