shopping cart

products = [ #Define product list
    ["Ipone",5800],
    ["Mac",15800],
    ["Coffee",30],
    ["Bike",2000],
    ["Cloth",500],
] 
shopping_list
= [] #empty shopping cart while True: #loop input salary salary = input("Your salary:") if salary.isdigit(): #Determine whether the input is an integer salary = int(salary) #Convert string to number break #Exit if executed correctly else: continue #Error loop execution while True: print("product list".center(50,"-")) #Print "product list" in the center with "-" as the fill center: center printing for index,i in enumerate(products): #Print product list, price print(index,".",i[0],i[1]) choice = input("Please enter the item number [quit]>>:") if choice.isdigit(): choice = int(choice) if choice >=0 and choice < len(products): #judgment list length: len #Determine if the money is enough p = products[choice] if salary >= p[1]:#Buy from salary -= p[1] #Deduct money shopping_list.append(p) #Add to cart print("Added \033[32;1m[%s]\033[0m into your shopping cart,and your current balance is \033[41;1m%s\033[0m" %(p[0],salary)) #彩色打印 else: print("Not enough money, you only have [%s]" % salary) else: print("There is no such item...") elif choice == "quit": #Enter quit to exit print("Purchased items".center(50,"-")) for i in shopping_list: print(i) #print shopping cart print("Your left balance is ", salary) #Print the remaining balance exit()

 

Guess you like

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