购物车练习程序(自己写的和老师讲的)自己的健壮性不好

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车练习程序(自己写的和老师讲的)自己的健壮性不好相关的知识,希望对你有一定的参考价值。

#Author:AXIN
#Date:2017/5/21 17:07
#输入工资
#打印出商品号和价格
#用户根据商品号来选择商品,够就直接扣钱,不够就提醒不够,退出购买
#用户随时可以退出(按q),退出时,打印已经购买的商品和余额


salary = int(input(‘Please input your salary : ‘))
# print(type(salary))
commodity_list = [‘1.iphone se ---- 3288 RMB‘,
                  ‘2.ipad ---- 2688 RMB‘,
                  ‘3.G-shock ---- 1095 RMB‘,
                  ‘4.BlueTooth Headset ---- 59 RMB‘]
c_price = [3288,2688,1095,59]
for i in range (4):
    print(commodity_list[i])

shopping_num = []#存放用户所选的商品号
# shopping_num.append(int(input(‘Please input your wanna commodity number : ‘)))
i = -1
#
# print(shopping_num[i])
# print(type(shopping_num[i]))
# shopping_num.append(int(input(‘Please input your wanna commodity number : ‘)))
# print(shopping_num)
# print(c_price[shopping_num[i]])
# print(type(c_price[shopping_num[i]]))
mod = ‘a‘
while mod !=‘q‘:
    shopping_num.append(int(input(‘Please input your wanna commodity number : ‘)))
    i= i+1
    if salary >= c_price[shopping_num[i]-1]:
        salary = salary - int(c_price[shopping_num[i]-1])
        print("You have already purchased the item :", shopping_num)
        mod = input(‘If you wanna exit ,input q .Press any key to continue‘)
    else:
        print(‘Your salary is not enough ...‘)
        shopping_num.pop()
        print("You have already purchased the item :", shopping_num)
        break

print("Your balance :",salary)

  

#Author:AXIN
#Date:2017/5/21 19:35
#老师讲的
product_list = [
    (‘Iphone‘,5288),
    (‘Mac pro‘,12000),
    (‘Bike‘,800),
    (‘Watch‘,36000),
    (‘Coffe‘,39),
    (‘Python book‘,120),
]
shopping_list = []

salary = input(‘Input your salary : ‘)
if salary.isdigit():#判断输入的是否是数字,如果是数字,把它变成int类型的
    salary = int(salary)
    while True:
        # for item in product_list:
        #     print(product_list.index(item),item)
        for index,item in enumerate(product_list):#enumerate 能把下标取出来
            print(index,item)

        user_choice = input("Your wanna product ?>>>")
        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 %s " %(p_item,salary))

                else:
                    print(‘\033[41;1mYour balabce only [%s] !\033[0m‘%salary)
            else:
                print("Product code [%s] is not exist !"%user_choice)
        elif user_choice ==‘q‘:

            print(‘--------------shopping_list---------------‘)
            for p in shopping_list:#python 中直接使用的变量初始化的 就是0,不用定义再使用
                print(p)
            print(‘Your balance :‘,salary)
            print(‘exit....‘)
            exit()
        else:
            print(‘Invalide choice ‘)

  

以上是关于购物车练习程序(自己写的和老师讲的)自己的健壮性不好的主要内容,如果未能解决你的问题,请参考以下文章

JAVA set get方法自己写的时候是先set 在写get

python实际练习1——简单购物车

一个简单的购物车程序

python-购物车程序练习

代码大全阅读笔记03

节流--自己写的函数