博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python作业之购物车
阅读量:7138 次
发布时间:2019-06-28

本文共 6978 字,大约阅读时间需要 23 分钟。

实现一个购物车的程序

1、统计每个物品的单价和总价和数量

2、判断钱是否够

shop_house = ["小米手机:2001", "苹果手机:5330", "三星手机:4103", "华为手机:3125", "魅族手机:3973", "联想手机:222", "酷派手机:155", "退出", "买单"]wel = "welcome to Cevin phone shopping house"total_money = 120000shop_car_dict = {}iphone_num = 0xiaomi_num = 0sanxing_num = 0huawei_num = 0meizu_num = 0lianxiang_num = 0kupai_num = 0while 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")

  

转载于:https://www.cnblogs.com/bainianminguo/p/6740740.html

你可能感兴趣的文章
在JDBC中使用Java8的日期LocalDate、LocalDateTime
查看>>
log4j MDC用户操作日志追踪配置
查看>>
react-native 项目更名步骤
查看>>
Java多线程——<五>后台线程(daemon)
查看>>
Linux 下安装PHPunit
查看>>
delphi中接口的委托和聚合
查看>>
优化反射性能的总结(上)
查看>>
HDU 2845 Beans
查看>>
ncl 实例参考
查看>>
SqlMetal Builder V2版本
查看>>
C#中数组与ArrayList的简单使用
查看>>
Activitys, Threads, & Memory Leaks
查看>>
poj3308Paratroopers(最小割)
查看>>
关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream解决办法
查看>>
携程面试之后的一些感想
查看>>
[收藏] 如何阅读别人的代码
查看>>
09年全年总结
查看>>
如何实现两个人脸照片的变换
查看>>
Bigtable:一个分布式的结构化数据存储系统
查看>>
Visual Studio OpenGL 配置方法
查看>>