Python simple Mall Shopping Cart instance code, python Mall Shopping Cart
This article will share with you the python code of a simple Shopping Mall Shopping cart for your reference. The specific content is as follows:
Requirements:
1. Write a piece of Shopping Cart order code for the mall.
2. Store the mall's product list in the list shopping_mail
3. the shopping cart list is shopping_cart.
4. the user first enters the salary amount and determines whether the input is a number.
5. the user selects the purchased item from the shopping list of the mall. After each purchase is successful, the user uses the salary minus the unit price of the item.
6. Check whether the user amount is sufficient. If not, prompt the user
7. If you do not want to continue shopping, press Q to exit the menu.
8. After the user exits, print out the list purchased by the user.
9. Try to learn to output several key characters with colors.
Code
# Author: show530shopping_mail = [('iphone ', 5800), ('mac Pro', 9800), ('bike', 800), ('Watch', 10600 ), ('coffee ', 31), ('Alex python', 120),] shopping_cart = [] salary = input ("Enter your salary:") if salary. isdigit (): salary = int (salary) while True: for index, item in enumerate (shopping_mail): print (index, item) enter_number = input ("Enter the product number you want to purchase:") if enter_number.isdigit (): enter_number = int (enter_number) if enter_numb Er>-1 and enter_number <len (shopping_mail): p_item = shopping_mail [enter_number] if p_item [1] <= salary: shopping_cart.append (p_item) salary-= p_item [1] print ("the purchased item amount is \ 033 [31 m % s \ 033 [0 m, and your salary balance is: \ 033 [31 m % s \ 033 [0 m "% (p_item, salary) else: print (" your balance is insufficient and you cannot continue purchasing! ") Else: print (" the product number you entered does not exist: ") elif enter_number =" q ": print (" \ 033 [0; 32; 40 m ------ your shopping list is ------- \ 033 [0 m ") for p in shopping_cart: print (p) print (" your balance is: \ 033 [31 m % s \ 033 [0 m "% (salary) exit () else: print (" \ 033 [31m input error, must enter a number or a positive integer! \ 033 [0 m ")
This program took me a lot of time, and I was thinking about it all night.
1. salary. isdigit () is not very familiar with this method. The function of this method is to check whether the user inputs a number. However, if the user inputs a floating point number, an error is returned. Negative Numbers also seem to report errors. Click here to swallow the dates.
2. I am not familiar with multi-layer nested loops. I started to output a problem because the nested format is incorrect. Before writing a program, draw a flowchart.
3. Learned How To use color output.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.