Program Exercises
Program: Shopping Cart Program
Demand:
- After you start the program, let the user enter the payroll, and then print the list of items
- Allow users to purchase items based on their product number
- After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
- You can exit at any time to print the purchased goods and balances when exiting
1 #-*-coding:utf-8-*-2 #!/usr/bin/env python3 #AUTHOR:DC4 5List_of_product = [["Iphonex", 8999], ["Apple Air", 3088], ["MacBook Air", 5999], ["Macbookpro", 128000],6["Iphone6", 4999]]7List_of_shopping = []8Money_of_guest = input ("Please enter your pre-deposit:")9 ifmoney_of_guest.isdigit ():TenMoney_of_guest =Int (money_of_guest) One whileTrue: A - Print("\ n The store product list is as follows \ n") - the forLinchlist_of_product: - Print(List_of_product.index (L), L) - -Num_product = input ("Please enter the serial number of the item you need to purchase:") + - ifnum_product.isdigit (): +Num_product =Int (num_product) A at ifMoney_of_guest < list_of_product[num_product][1]: - Print("your balance is not sufficient to purchase this item, please select another item or enter Q to exit") - Else: - Print("added {name_of_product} to your shopping cart". Format (name_of_product=list_of_product[num_product][0])) -Money_of_guest = money_of_guest-list_of_product[num_product][1] - Print("your balance also has {B}". Format (b=money_of_guest),"Yuan") in list_of_shopping.append (list_of_product[num_product]) - elifNum_product = ="Q": to Print("\ n Your shopping list is as follows: \ n") + forIinchlist_of_shopping: - Print(i) the Print("your balance also has {B}". Format (b=money_of_guest),"yuan, welcome to come again next time! ") * exit () $ Else:Panax Notoginseng Print("Please enter the correct amount")
View Code
python-Shopping Cart Program