实现一个购物车的程序
1、统计每个物品的单价和总价和数量
2、判断钱是否够
shop_house = ["小米手机:2001", "苹果手机:5330", "三星手机:4103", "华为手机:3125", "魅族手机:3973", "联想手机:222", "酷派手机:155", "退出", "买单"] wel = "welcome to Cevin phone shopping house" total_money = 120000 shop_car_dict = {} iphone_num = 0 xiaomi_num = 0 sanxing_num = 0 huawei_num = 0 meizu_num = 0 lianxiang_num = 0 kupai_num = 0 while True: print(wel.center(80, '-')) for i, phone in enumerate(shop_house): print(i, phone) option = input("which phone do you want to buy?") if option == "0": per = shop_house[0].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): xiaomi_num += 1 per_dict = {per[0]: {"总价": int(per[1])*xiaomi_num, "数量": xiaomi_num,"单价":int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: xiaomi_num = 1 per_dict = {per[0]: {"总价": int(per[1])*xiaomi_num, "数量": xiaomi_num,"单价":int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "1": per = shop_house[1].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): iphone_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * iphone_num, "数量": iphone_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: iphone_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * iphone_num, "数量": iphone_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "2": per = shop_house[2].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): sanxing_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * sanxing_num, "数量": sanxing_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: sanxing_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * sanxing_num, "数量": sanxing_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "3": per = shop_house[3].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): huawei_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * huawei_num, "数量": huawei_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: huawei_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * huawei_num, "数量": huawei_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "4": per = shop_house[4].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): meizu_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * meizu_num, "数量": meizu_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: meizu_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * meizu_num, "数量": meizu_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "5": per = shop_house[5].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): lianxiang_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * lianxiang_num, "数量": lianxiang_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: lianxiang_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * lianxiang_num, "数量": lianxiang_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "6": per = shop_house[6].split(":") if total_money >= int(per[1]): total_money = total_money - int(per[1]) # print('-'.center("50")) print("%s is in shop_car,and you money is %d" % (per[0], total_money)) if shop_car_dict.get(per[0]): kupai_num += 1 per_dict = {per[0]: {"总价": int(per[1]) * kupai_num, "数量": kupai_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: kupai_num = 1 per_dict = {per[0]: {"总价": int(per[1]) * kupai_num, "数量": kupai_num, "单价": int(per[1])}} shop_car_dict.update(per_dict) print(shop_car_dict) else: print("sorry your money is not enough!") elif option == "7": while True: option = input("are you sure to exit?\n1:退出\n2:返回") if option == "1": print("ByeBye") exit() elif option == "2": break else: print("input error") continue elif option == "8": if shop_car_dict: print("you money is %d" % (total_money)) print(shop_car_dict) exit() else: print("you have buy nothing") print("you money is %d" % (total_money)) exit() else: print("404")