要求:
1.让用户输入工资
2.输出购物菜单及产品价格
3.计算用户是否可支付
4.输出用户剩余的钱,问用户是否继续购物,如果选择继续 ,继续进行,只到钱不够为止。

#!/usr/bin/env python
gongzi = 5000
import sys
caidan = '''
====================
A:Iphone : 5888
B:Computer: 11000
C:Xiaomi:  2499
D:Mouse :  129
E:TV : 2499
Q:quit
====================
What would you want to buy?
'''

panduan = 0
things=['A','B','C','D','E']                        ##代号
price=['5888','11000','2499','129','2499']          ##价格
stuff=['Iphone','Computer','Xiaomi','Mouse','TV']   ##物品名称
have=[]
while True:                                         
    print caidan                                    ##打印菜单
    anser = raw_input('you can input "A" or "a": \n').upper()       ##输入要购买的物品
    if anser == "Q":                                      ##如果选择退出
        print "You have bought below stuff:"
        print have
        sys.exit('Good Bye!')
    if anser not in things:                                      ##如果选择不存在的物品
        print "you's input is wrong,please input 'a' or 'A'!"  
        continue
    for i in things:                                             
        if anser == i:                                         ##判断选择的物品对应的价格
            a = things.index(anser)
            if int(gongzi) >=  int(price[a]):                  ##可以购买时
                b = int(gongzi) - int(price[a]) 
                print "OK! You buy success! You have %s yuan now!" %b
                have.append(stuff[a])                           ##购买成功的物品添加进已拥有列表
                print 'Do you want to buy go on?'                
                on = raw_input('please input no or other:')      ##判断是否需要继续购物
                if on == 'no':
                    print "You have bought below stuff:"
                    print have
                    sys.exit("Bye-Bye!")
                gongzi = int(b)
            if int(gongzi) < int(price[a]):                        ##不够支付时
                print "Sorry! this things need %s ! you have %s!" %(price[a],gongzi)
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐