ShoppingCart脚本

Python Liemer_Lius 1094℃ 0评论

功能:

  • 输入自己的现金金额
  • 选择要买的物品
  • 扣除金额后退出, 打印自己购物车里面的商品和余额
# _Author: lWX548594
# _Date: 2018/6/23 0023
# _SctiptName: shoppingcar

# 输入saving,并判断是否合法,不合法则退出;
saving = input("Please input your saving: ")
if not saving.isdigit():
    print("Please input a number. Now exit...")
    exit()
else:
    saving = int(saving)

# 打印商品列表;
goods = [('iphone X', 9000), ('Tesla', 900000), ('Python Book', 105), ('Kindle', 800), ('Coffee', 32)]
len_of_goods = len(goods)
shopping_cart = []

# 开始循环
choose_flag = False
while True:
    # 打印商品列表
    for i in enumerate(goods, 1):
        print("%s: %s" % (i[0], i[1]))

    # 判断商品的ID是否合法,不合法提示输入的范围
    choice = input("Please input the index of goods, 'q' or 'quit' for quit:")
    if choice == 'q' or choice == 'quit':
        choose_flag = True
        break
    if choice.isdigit():
        choice = int(choice)
    else:
        print("Please input a number between 1 - %s !" % len_of_goods)
        continue
    if choice - 1 not  in range(len_of_goods):
        print("Please input a number between 1 - %s !" % len_of_goods)
        continue
    # 判断商品价格是否小于saving,并将商品加入购物车
    else:
        goods_price = goods[choice-1][1]
        if goods_price <= saving:
            saving -= goods_price
            shopping_cart.append(goods[choice - 1][0])
        else:
            print("You donn't have enough money.")
if choose_flag:
    if len(shopping_cart) == 0:
        shopping_cart.append('None')
    print("End.... You have bought:")
    for j in shopping_cart:
        print(j)
    print("Your saving remains: %s" % saving)

执行结果展示:

C:\Users\lwx548594\PycharmProjects\LiusProject\venv\Scripts\python.exe C:/Users/lwx548594/PycharmProjects/LiusProject/old/shoppingcar.py
Please input your saving: 9000000
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:1
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:2
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:3
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:4
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:5
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:6
Please input a number between 1 - 5 !
1: ('iphone X', 9000)
2: ('Tesla', 900000)
3: ('Python Book', 105)
4: ('Kindle', 800)
5: ('Coffee', 32)
Please input the index of goods, 'q' or 'quit' for quit:q
End.... You have bought:
iphone X
Tesla
Python Book
Kindle
Coffee
Your saving remains: 8090063

Process finished with exit code 0

 

转载请注明:liutianfeng.com » ShoppingCart脚本

喜欢 (0)

发表回复