python实现购物车

购物车的题目要求:

python实现购物车_第1张图片

代码逻辑:

#Author:Kelly
shopping_list=[]
listGoods = [("iphone",1000),("bike",400),("mac",300)]
salary = input("Please input your sarlary:")
if salary.isdigit():
    salary = int(salary)
# for item in listGoods:
#     print(listGoods.index(item),item)#取下标
    while True:
        for index,item in enumerate(listGoods):#取下标
            print(index,item)
        user_choice = input("please choose your things:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice <= len(listGoods) and user_choice >= 0:
                p_item = listGoods[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"%(user_choice,salary))
                else:
                    print("余额不足!")
            else:
                print("所选的商品无效!")
        elif user_choice == 'q':
            print('--------shopping list-------------!')
            for p in shopping_list:
                print(p)
            print("所剩余额是: %s"%(salary))
            break
        else:
            print("invalid option")
else:
    print("invalid option")

遇到的问题:

1、报错:TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

问题:

解决方案:

 python3:%(变量名,...)应该是加在print括号里的

运行如下:

python实现购物车_第2张图片

你可能感兴趣的:(python实现购物车)