Python basic day02—training1_ shopping cart program practice

Self-study practice, record it, if there is something wrong, welcome everyone to point it out!

Program: Shopping Cart Program

need:

  1. After starting the program, let the user enter the salary and then print the list of items
  2. Allow users to purchase items based on item numbers
  3. After the user selects the product, check whether the balance is enough, and directly deduct the money if it is enough, and remind if it is not enough. 
  4. You can exit at any time, when you exit, print the purchased items and the balance

 

Code:

#cherry_cui

mall_list = [
    ('Bicycle',800),
    ('computer',4800),
    ('pizza',50),
    ('skirt',200),
    ('shoes',1200),
    ('iphone6',3200),
    ('watch',2700)
]
shopping_cart =[]
budget = input( " Input your budget: " ) #Enter the shopping budget
 if budget.isdigit(): #Determine whether the budget consists only of numbers.
    budget = int (budget) #Convert to int type

    while True:
         for num,item in enumerate(mall_list): # enumerate() gets the subscript of mall_list
            print(num,item)

        choice = input( " which goods you want(enter the number): " ) #Enter the number of the goods you want to buy
         if choice.isdigit(): #Determine whether choice consists of numbers only.
            choice = int (choice) #Convert to int type

            if choice < len(mall_list) and choice >= 0 : #len() method returns the number of elements in the mall_list list
                u_item = mall_list[choice]
                if u_item[1]<=budget:
                    budget-=u_item[1]
                    shopping_cart.append(u_item) #Add the purchased item to the shopping cart shopping_cart
                    print("%s had added to your shopping list,your current balance is %s !" %(u_item[0],budget))

                else:
                    print("Your current balance is %s ,not enough to buy %s ,choose again!" %(budget,u_item[0]))

            else:
                print("Product code %s is not exist,choose again" %choice)


        elif choice == 'q':
            print("-----shopping_cart-----")
            for c in shopping_cart:
                print(c)

            print("Your current balance is %s" %budget)
            exit()


        else:
            print("Input error!")
            exit()


else:
    print("Input error!")
View Code

 

Guess you like

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