Proposition:
- After you start the program, let the user enter the payroll, and then print the list of items
- Allow users to purchase items based on their product number
- After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
- 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