加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

购物

发布时间:2020-12-17 17:29:57 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env pythonimport sys'''简单的购物菜单,根据输入的金额,减去选择的商品得到剩下的金额,一旦省下的金额小于所有的商品价格,提示余额不

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/env python
import sys
'''简单的购物菜单,根据输入的金额,减去选择的商品得到剩下的金额,
一旦省下的金额小于所有的商品价格,提示余额不足'''
product = []
price = []
f = open('act.txt')
for line in f.readlines():
    p = line.split()[0]
    pp = line.split()[1]
    product.append(p)
    price.append(int(pp))
f.close()
shop_list =[]
while True:
    try:
        money = int(raw_input("How money you have:"))
        break
    except ValueError:
        print "wrong enter"
while True:
    for p in product:
        print p,price[product.index(p)]
    print "buy sth~"
    choose = raw_input("please enter which one you buy:").strip()
    if choose == "quit":
            sys.exit()
    if choose in product:
            product_price = price[product.index(choose)]
            if money > product_price:
                shop_list.append(choose)
                print "Add %s into your basket" % choose
                money = money - product_price
                print "you have %s money to buy sth" % money
            else:
                if money < min(price):
                    print "go home baby,there is no money to afford sth"
                    print "%s to buy successful" % shop_list
                    sys.exit()
                else:
                    print "you can't afford this product"

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读