Python之路购物车

#coding:utf-8
wages=raw_input('what are your wages:')
lise_commodity=[('apples',50),('orange',55),('pear',35)]
shopping_list=[]
if wages.isdigit():
    salary=int(wages)
    while True:
        for index,i in enumerate(lise_commodity):
            print index,i
        user_choice=raw_input('are you want?')
        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice <=len(lise_commodity) and user_choice >=0:
                p_item=lise_commodity[user_choice]
                if p_item[1]<=salary:  #买的起。
                    shopping_list.append(p_item)
                    salary-=p_item[1]
                    print '已添加到购物车'
                else:
                    print '33[41余额不足,exit....33[0m'
                    print shopping_list
                    break
                         
        elif user_choice=='q':
            print '------shopping ------'
            for index,i in enumerate(shopping_list):
                print index,i
            print '余额为:',salary
            break

  

本博客只记录个人的学习过程,如对文中有不懂的地方,可以在评论区留言!
原文地址:https://www.cnblogs.com/iexperience/p/9002018.html