代码改变世界

购物车作业

2018-03-02 23:03  JohannSlash  阅读(124)  评论(0)    收藏  举报

购物车作业,在没有看答案的情况写编写,运行成功,但是chois输入为空时程序报错

# -*- coding: utf-8 -*-
"""
Created on Fri Mar  2 22:08:08 2018

@author: yh
"""

exit_flag = 1
list_num=5
good_list = [["IPhone",5800],
             ["Mac Pro",12000],
             ["Starbuck",31],
             ["Alex Pythn",81],
             ["Bike",800]]
your_list=[]
salary=int(input("Plese input your salary:"))
while exit_flag == True and list_num>0:
    print("welcome, the goods list is")
    for i in range(list_num):
        print(i+1,"\t",good_list[i][0],"\t",good_list[i][1])
    choise=input("Please input your choise:")
    if choise != "q":
        choise=int(choise)
        print("your choise is", choise, good_list[choise-1])
        salary-=good_list[choise-1][1]
        if salary >0:
            your_list.append(good_list[choise-1])
            print("your rest salary is",salary)
            good_list.pop(choise-1)
            list_num-=1
        else:
            salary+=good_list[choise-1][1]
            #exit_flag=0
            print("not enogh salary")
    elif choise == "q":
         exit_flag=0
         #print("not enogh salary")
else:
    print("exit,your list is ")
    for j in your_list:
        print(j)
    print("your rest salary is", salary)