python實現購物車

購物車的題目要求:python

代碼邏輯:git

#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")

遇到的問題:app

一、報錯:TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'iphone

問題:code

解決方案:blog

 python3:%(變量名,...)應該是加在print括號裏的ip

運行以下:input