1. Basic Requirements
Write a shopping applet that asks for the following features:
(1) To allow users to enter wages;
(2) Export shopping menu and product price;
(3) Calculate whether the user can pay;
(4) Output the user's remaining money, ask the user whether to continue shopping, if you choose to continue, then continue, or exit the program;
(5) If the money is not enough, the output user still need to work how long to buy (this feature is not implemented here).
2. Realizing the basic idea
The basic idea can be as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/02/wKioL1XyQXaQFd6HAAHEHjynTRI164.jpg "title=" Shop.jpg "alt=" Wkiol1xyqxaqfd6haahehjyntri164.jpg "/>
In the process of writing the idea as the main line, the specific details mentioned below.
3. Implementation Details
Based on the principles of the friendly user interface, the implementation details can be summarized as follows:
(1) When the user enters the salary, if the input is not a number or no input, there will be prompted to enter again, and will not exit abnormally;
(2) When the user enters the purchase item, if the entered non-commodity index or the non-quit quit, there will be prompted to enter again, and will not exit abnormally;
(3) After each added item to the shopping list, the user will be prompted to the current remaining money;
(4) When the user chooses to exit, the user's shopping list is printed, and the remaining money of the user is output;
(5) The general principle is that the program will not appear in the execution of abnormal conditions, even if the user entered illegal characters.
4. Implementing Code and annotations
The implementation code based on the above requirements is as follows:
#!/usr/bin/env pythonimport sysmarket = [[' Xiaomi phone ', 2400],[' Iphone ', 6000],[' computer ', 3800],[' Ipad ', 1800],[' Core python ', 69],[' Router ', 109]]shop_list = [] #The shopping list of the buyer.while true: salary = raw_input (' Please input your salary per month: '). Strip () if not salary.isdigit (): print ' please enter your salary. ' continue salary = int (Salary) print "Welcome to our market! you can buy something cool here, or you can enter ' Quit ' to left.\n " break#upon:input the salary per month. Enter the salary and avoid abnormal situations. while true: print ' The goods we serve are as follow: ' &Nbsp;for goods in market: print market.index (goods), goods[0],goods[ 1] #Upon:p rint the goods list. choice = raw_input (' What do you want to buy? ') if choice == ' quit ': # ' quit ' system print ' \nyour shopping list are as follow: ' for goods in shop_list: print goods[0],goods[1] print ' now You have %d left. ' % (Salary) sys.exit (' goodbye! ') elif len (choice) == 0:# ' enter ' system continue# actually ' Luanma ' system and ' enter ' system can only be replaced with the former, here just want to output a different hint. (That is, you are prompted when you enter garbled characters, and you do not prompt when entering enter.) ) if not choice.isdigit (): # ' Luanma ' system, that is, if the user enters a non-digit, there will be a hint Print ' Please input the right choice. (Number to buy things and quit to quit.) \ n ' continue #Upon: ' quit ' system , ' enter ' system and ' Luanma ' handle system. choice = int (choice) if choice >= len (market): print ' Could not find the item, try again!\n ' continue pri = market [choice] #Upon: To check if the number is legal. Confirm that the number entered is within the legal range pri = market[choice] if pri[1] <= salary: salary = salary - pri[1] #The remaining money . shop_list.append (PRI) #Adding the goods to the list. print ' you have added %s to Your shopping list, and you have %d left.\n ' % (pri[0], Salary) else: print ' You have %d left. you can not afford to buy %s, but you can try to Buy other things.\n ' % (salary, pri[0])
Because of the flexibility of using lists for processing, the code is relatively less complex.
4. Testing
• The various situations and descriptions that may occur during the input process are as follows:
[Email protected]:/mnt/hgfs/python/day2$ python myshop.py please input your salary per month:Please enter your salary. ===> Direct Input ' Enter ' will be prompted please input your salary per month:klkdfplease enter your Salary. ===> input garbled will also be prompted Please input your salary per month : 10000welcome to our market! you can buy something cool here, or you can enter ' Quit ' to left. The goods we serve are as follow: ===> first printed a product menu 0 Xiaomi Phone 24001 Iphone 60002 Computer 38003 Ipad 18004 core python 695 router 109what do you want to buy?0 ===> correct input of the product index number, will prompt the current user shopping information You have added xiaomi&nbsP Phone to your shopping list, and you have 7600 left. The goods we serve are as follow:0 xiaomi phone 24001 iphone 60002 Computer 38003 Ipad 18004 Core Python 695 Router 109what do you want to buy? ===> only Enter ' Enter ', there is no prompt output, But will print the item menu again the goods we serve are as follow:0 xiaomi phone 24001 Iphone 60002 Computer 38003 Ipad 18004 Core Python 695 Router 109what do you want to buy?3you have added ipad to your shopping list, and you have 5800 left. The goods we serve are as follow:0 xiaomi phone 24001 iphone 60002 computer 38003 ipad 18004 core Python 695 Router 109What do you want to buy?eqwer ===> input garbled, there will be a prompt, and accept the input please input the right choice again. (Number to buy things and quit to quit.) The goods we serve are as follow:0 xiaomi phone 24001 iphone 60002 Computer 38003 Ipad 18004 Core Python 695 Router 109what do you want to buy?9 ===> If you enter a number that is not in the legal range, you will also be prompted could not find the item, try again! The goods we serve are as follow:0 xiaomi phone 24001 iphone 60002 Computer 38003 Ipad 18004 Core Python 695 Router 109what do you want to buy?quit ===> normal exit, will print the user's total shopping list information yOur shopping list are as follow:xiaomi phone 2400ipad 1800now you have 5800 left. goodbye!
Relatively speaking, the user interface is relatively friendly, because there will not be too much abnormal situation, but also to achieve the basic requirements.
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1693767
"Python Tour" chapter II (III): List-based shopping list process