Python Learning (8) Example: Write code for a simple mall shopping cart

Source: Internet
Author: User

Requirements:

1, write a shopping cart sequence code
2. Use the list to store the product list of the mall and save it to the list Shopping_mail
3, the shopping cart list is Shopping_cart
4, the user first enter the salary amount, judge the input as the number
5, the user from the mall shopping list to choose to buy items, each time the purchase of success with wages minus the item unit price
6, determine whether the user amount is sufficient, not enough to prompt the user
7. If the user does not want to continue shopping, press the Q key to exit the menu
8. After the user exits, print out the list that the user purchased.
9, as far as possible to learn with color output several key information characters.

1 #author:show5302 3Shopping_mail = [4('Iphone', 5800),5('Mac Pro', 9800),6('Bike', 800),7('Watch', 10600),8('Coffee', 31),9('Alex Python', 120),Ten ] OneShopping_cart = [] A  -Salary=input ("Please enter your salary amount:") - ifsalary.isdigit (): thesalary=Int (Salary) -     whileTrue: -         forIndex,iteminchEnumerate (shopping_mail): -            Print(Index,item) +Enter_number=input ("Please enter the product number you want to purchase:") -        ifenter_number.isdigit (): +Enter_number=Int (enter_number) A            ifEnter_number>-1 andenter_number<Len (shopping_mail): atp_item=Shopping_mail[enter_number] -                ifp_item[1]<=Salary: - shopping_cart.append (P_item) -Salary-=p_item[1] -                    Print("The amount of your purchase is: \033[31m%s\033[0m, your salary balance is: \033[31m%s\033[0m"%(P_item, salary)) -                Else: in                    Print("your balance is not enough to continue your purchase! ") -            Else: to                  Print("The item number you entered does not exist:") +        elifenter_number=="Q": -            Print("\033[0;32;40m------You This shopping list is-------\033[0m") the             forPinchShopping_cart: *                Print(P) $            Print("your balance is: \033[31m%s\033[0m"%(Salary))Panax Notoginseng exit () -        Else: the                Print("\033[31m input Error, you must enter a number or a positive integer! \033[0m")

It took me a lot of time to think about it for almost a night.

1, Salary.isdigit () is not too familiar with, the role of this method is to check whether the user input is a number, but do not know why, the input floating point will be an error. Negative numbers appear to be an error. Here swallowed first.

2, for multilayer nested loops are not familiar with the beginning of the output has been the problem, the main reason is that the nested format is not correct. Be careful to draw a flowchart before you write your program.

3, learn the use of color output.

Python Learning (8) Example: Write code for a simple mall shopping cart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.