I. Scripting requirements
After starting the program, let the user enter the payroll, and then print the list of items;
Allows the user to purchase goods according to the product number;
After the user selects the product, checks whether the balance is enough, enough on the direct debit, insufficient on the reminder;
Checkout cart, print purchased goods and balances;
May withdraw from the program at any time;
Two. Knowledge points related to Python
Comments
Variable
User input
Listing (list)
Tuple (tuple)
Numeric operations
If process statement
While Loop statement
For Loop statement
Three. Scripts
#!/usr/bin/env python3#author=guoxhmenu=[ (' iphone 7 ', 6300), (' Macbookpro ', 12000), (' Mechanical keyboard ', 380), (' python tutorial , (' cooker ', 650)]ps= ' ps: Enter the product into the shopping cart; enter Q or quit the shopping program; Enter W or W to settle the cart (showing the goods and balances that have been purchased); "' Shoppingcart=[]salary=input (' Please enter your payroll: ') if Salary.isdigit (): salary=int (Salary) print ("--------shopping list------") for q in menu: print (Menu.index (q), q) print (PS) while true: choice=input (' Please select the product number you want to buy: ') if choice.isdigit (): choice=int (choice) if choice< len (menu): if menu[choice][1 ] < salary: &Nbsp; shoppingcart.append ( Menu[choice]) salary=salary-menu[choice][1] print ("You have already", menu[choice][0], "Add to cart, your balance is:", salary) else: print ("Your balance is:", salary, ", cannot afford:", menu[choice][0]) else: print (" The item you entered does not exist, please re-enter ... ") continue elif choice == ' W ' or choice == ' W ': if Shoppingcart: print ("Goods you have purchased:") print ("---------------") for i in shoppingcart: print (i[0]) print ("---------------") print (">>> You now have a total surplus:", salary) break elif choice == ' Q ' or choice == ' Quit ': Print ("You have exited the shopping process, this time did not buy any goods, welcome to visit again!") ") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BREAK&NBSP;&NBSP;&NBSP;&NBSp;else: print ("The item you entered does not exist, please re-enter!")
Four. Test scripts
Please enter your total wage: 10000--------Shopping List------0 (' Iphone 7 ', 6300) 1 (' Macbookpro ', 12000) 2 (' Mechanical keyboard ', 380) 3 (' Python tutorial ', 80) 4 ( ' Induction cooker ', 650 PS: Enter the number to add the item to the shopping cart; Enter Q or quit to exit the shopping process; Enter W or W to settle the cart (showing the goods and balances that have been purchased); Select the item number you want to buy: 0 You have added Iphone 7 to your cart and your balance is: 3700 Please select the item number you wish to purchase: 2 You have added the mechanical keyboard to your cart, and your balance is: 3320 Select the item number you want to buy: 1 Your balance is: 3320, cannot afford: Macbookpro Please select the item number you wish to purchase: 3 You have added the Python tutorial to your shopping cart, your balance is: 3240 Please select the item number you want to buy: 4 you have the cooker To add to your cart, your balance is: 2590 Please select the item number you wish to purchase: 5 The item you entered does not exist, please re-enter .... Please select the item number you want to buy: W you have purchased:---------------Iphone 7 mechanical Keyboard Python tutorial Cooker--------------->>> You now have a total of the remaining: 2590
This article is from "Big Brother next Door" blog, please be sure to keep this source http://guoxh.blog.51cto.com/10976315/1932110
Python Learning: Shopping Programs