DAY2 Exercise - Shopping Cart



print('Welcome to the shopping cart')
money = int(input('For the convenience of shopping, please enter your total assets: ')) #input money must be of digital type
shopping_price_list = [{"name": "computer", " price": 1000} ,
{"name": "mouse", "price": 10},
{"name": "yacht", "price": 20},
{"name": "beauty", "price" : 2000} ]
shopping_car = []
while True:
index = 0
for goods in shopping_price_list:
print(index, goods)
index += 1
choice = input('Please select the serial number: (if exit, please input q)').strip( ).lower()
# Determine whether the input is a number
if choice.isdigit():
choice = int(choice ) #Determine
the existence of the product
if choice >=0 and choice <len(shopping_price_list):
goods = shopping_price_list[choice]
count = int(input('Please enter the quantity you want to buy:'))
#Determine whether you can afford it
if goods['price'] *count <= money:
goods.setdefault('count',count)
shopping_car.append(goods)
money -= goods['price']*count
print('Currently purchased goods are: '+ goods['name'] + ' Purchase quantity: '+ str(count) + ' Current balance is: '+ str(money))

else:
money1 = goods['price']*count-money
print('Insufficient funds, there is still %s money'%money1)
break
else:
print("The item does not exist, please select again:")
elif choice == 'q':
print('------------Purchased item list--------- -------')
for i in shopping_car:
print(i)
print('The current balance is:',money)
break
else:
print("No such option")
---------- -------------------------------------------------- -------------------------------------------------- -------------
Change output color:

if goods['price']*count <= money: 
goods.setdefault('count',count)
shopping_car.append(goods)
money -= goods['price']*count
print(' \033[ 31 ;1m currently Purchase \033[0m goods: '+ goods['name'] + ' Purchase quantity: '+ str(count) + ' Current balance is: '+ str(money))---------this The method will change the output color of the currently purchased four words to red,
and change the 31 number to change a series of special effects such as color and shadow. Just give it a try! \033[1m represents the start character \033[0m represents the end character

------------------------------------ -------------------------------------------------- -------------------------------------






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324895494&siteId=291194637