python is used to implement simple cyclic shopping cart function
- 2020-06-07 04:49:28
- OfStack
This paper mainly introduces the python to realize the circular shopping cart function related content, sharing for your reference and learning, the following is a detailed introduction:
The sample code
# -*- coding: utf-8 -*-
__author__ = 'hujianli'
shopping = [
("iphone6s", 5000),
("book python", 81),
("iwach", 3200),
(" The TV ", 2200)
]
def zero(name):
if len(name) == 0:
print("\033[31;1m Your input cannot be empty, please enter again ~~\033[0m")
exit()
else:
return True
def crre(name):
if int(name) not in range(len(shopping)):
print("\033[31;1m You entered the wrong serial number, please reenter it ~~\033[0m")
exit()
else:
return True
gongzi = input(" Please enter your current salary: ")
if not gongzi.isdigit():
crre()
elif len(gongzi) == 0:
zero()
else:
gongzi = int(gongzi)
print("=========================================")
print("=== The list of items to buy is as follows: ")
for i,j in enumerate(shopping):
print(i, j[0], j[1])
shopping_cart = []
while True:
choice = input(" Please enter the item you want to buy , The input q or exit Exit to return to shopping cart list: ")
if choice.isdigit():
zero(choice)
crre(choice)
choice = int(choice)
goods = shopping[choice]
if gongzi < int(goods[1]):
gongzi -= goods[1]
print("=== Your salary balance is not enough. It's not enough \033[1;31m{}\033[0m Please work hard to make money ===.".format(abs(gongzi)))
else:
print(" The goods you choose :\033[1;32m %s \033[0m Added to cart .." % (goods[0]))
shopping_cart.append(goods)
gongzi -= goods[1]
print(" Your salary balance is left over :\033[30;42m %s\033[0m"% (gongzi))
elif (choice == "q") or (choice == "exit"):
break
print('\033[30;42m List of goods you have ordered \033[0m'.center(42,'-'))
for i,j in enumerate(shopping_cart):
print("\033[1;32m" + str(i+1) + " " + str(j[0]) + " " + str(j[1]) + "\033[0m")
conclusion