python 的 购物小程序

 1 money = input('请输入您的工资:')
 2 shop = [("iphone",5800),("ipod",3000),("book",210),("Archer python",80)]
 3 while not money.isdigit():
 4     print("请正确输入整数",end=':')
 5     money = input()
 6 money = int(money)
 7 while True:
 8     print("商品详情".center(50,'-'))
 9     for i in range(len(shop)):
10         print("%d. %s $%s".center(50,' ') % (i,shop[i][0],shop[i][1]))
11     print("请输入你要购买的商品序号,(退出请按'q'):",end='')
12     num = input()
13     while not( num == 'q' or num.isdigit() and int(num) < len(shop) and int(num) >= 0 ):
14         print("请正确输入商品序号:",end='')
15         num = input()
16     if num == 'q':
17         break
18     num = int(num)
19     if money >= int(shop[num][1]):
20         money -= int(shop[num][1])
21         print("您购买了%s 价格为%d --您还有余额为\033[31;1m$%d\033[1m" % (shop[num][0],shop[num][1],money))
22     elif money <= int(shop[num][1]):
23         print("您当前余额不足,剩余余额为:\033[31;1m$%d\033[1m"%(money))
shopping

 

posted @ 2018-09-20 17:54  罪途  阅读(265)  评论(0编辑  收藏  举报