当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 购买物品的程序(代码教程)

Python 购买物品的程序(代码教程)

2018年02月11日  | 移动技术网IT编程  | 我要评论

北苑家园茉莉园,2017环法直播,铜之家

要求:

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)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网