Python Exercises _ Easy Shopping cart

Source: Internet
Author: User

Proposition:

    1. After you start the program, let the user enter the payroll, and then print the list of items
    2. Allow users to purchase items based on their product number
    3. After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
    4. You can exit at any time to print the purchased goods and balances when exiting

Pseudo code

Shopping_car =[]balance= Input usr's Balance(Check parameter's legality) whileTRUE {Show Commodity list choice=user InputifChoice.isdigit {ifChoice >Number of commodity{Print("Invalid input")            Continue} Item=Commodity[choice]ifItem.price <Balance {shopping_car.append (item) Balance-=Item.pricePrint("Item Add success, balance:%d")        }        Else{            Print("balance is not enough!")        }    }    Else{        ifChoice = ='Q'{            Print("your shopping things & balance")             Break        }Else {            Print("Invalid input")        }    }}

Source:

#!/usr/local/bin/python3.5Importsyscommodity= [    ("iphone", 5888),    ("MacBook", 12888),    ("Starbark", 60),    ("Bike", 1000)]shopping_car= [] whiletrue:balance= Input ("Input your balance:")    ifBalance.isdigit ():#Check if it's plastic.balance = Int (balance)#type conversion Shaping         Break    Else:        ifBalance = ='Q': Sys.exit (0)Else:            Print("----->invalied Input!!!") whileTrue: forIndex, CLinchEnumerate (commodity):#for list, the function converts it to an index sequence        Print("\ n", index, CL) UC= Input ("Input your choice:")#Input Selection    ifuc.isdigit (): UC=Int (UC)ifUC < Len (commodity) andUC >=0:item=Commodity[uc]ifITEM[1] <=balance:shopping_car.append (item) Balance-= Item[1]                Print("%s Add shopping car sucess!"%item[0])Else:                Print("your balance is not enough!")        Else:            Print("------->invalid input!")    elifUC = ='Q':        Print("----------Shopping list-----------")         forCinchShopping_car:Print(c)Print("your balance:%d"%balance) Break; Else:        Print("------->invalid Option")

Python Exercises _ Easy 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.