This article to you to share is the use of Python to implement the concept of shopping small program requirements and related code, very simple and practical, the need for small partners can refer to the following
Requirements:
After starting the program, let the user enter a salary, and then print out a list of items with serial numbers
The user enters the product serial number to purchase the corresponding product, or enters ' Q ' to exit the purchase interface
After selecting the goods, check whether the balance is enough, enough to direct debit, not enough to suggest that the balance is insufficient
After each purchase of a product, or enter ' Q ' after exiting the purchase interface, you are prompted: Do you want to continue to purchase? (y/n) for multiple purchases
If a user buys a product, prints a list of items purchased, the total amount, and the balance; if the user does not buy any goods, print: End of transaction, shopping failure
Readme:
Run the program, enter your salary, select the item you purchased according to the serial number of the product list, you can choose multiple purchases, or do not purchase
Flow chart:
Code:
# simple Shopping applet product_list = [[' Surface Pro 4 ', 7800], [' Dell XPS + ', 12000], [' MacBook ', 12000], [' Xiaomi 6 ', 2499], [' IPhO Ne7 ', 4600], [' Nut pro ', 1499]]shopping_list = []# determines if the pay format entered is correct while true:salary = input (' \ n Please enter your salary: ') if not salary.is Digit (): # salary is not a number, end loop print (' \ n input format is wrong! Please re-enter ... ') continue breakbalance = salary = Int (salary) print (' \ n-----------Welcome to purchase------------\ n ') # Generate a list of products with serial number for index, Item in Enumerate (product_list): Print (index, item) # Determines whether the input sequence number meets the requirements while true:while true:i = input (' \ n Enter the serial number of the item you want to buy, or into q Cancel purchase: ') if i = = ' Q ': # Enter Q to exit the purchase interface while TRUE:A = input (' \ n continue to purchase? (y/n): ' If a! = ' n ' and a! = ' n ' and a! = ' Y ' and a! = ' Y ': print (' \ n input format is wrong, please try again ... ') continue elif a = = ' Y ' or a = = ' Y ': # Continue buying break else: # purchase complete if balance = = Salary: # did not buy anything print (' \ n trade ended, purchase failed ... ') exit () Else: # Checkout Print (' \ n ' You have successfully purchased the following goods: \ n ') for item in Shopping_list:print (item) print (' \ n total consumption Amount%d, balance%d yuan '% (salary-balance, balance)) exit () Continue if not I.isdigit (): # ordinal is not a number Word, end loop print (' \ n input format is incorrect! Please re-enter ... ') Continue i = Int (i) if I < 0 or I >= len (product_list): # range incorrect, end loop print (' \ n This item does not exist, please Re-enter ... ') Continue break Product = product_list[i] Price = Int (product[1]) # Determine if the balance is sufficient to direct debit, enough to remind if Price < = balance:balance-= Price Shopping_list.append (product_list[i]) print (' \ n you have successfully purchased%s, the current balance is%d '% (product, Bal ance)) Else:print (' \ n purchase failed, your balance is insufficient ... ') while true:a = input (' \ n continue to purchase? (y/n): ' If a! = ' n ' and a! = ' n ' and a! = ' Y ' and a! = ' Y ': print (' \ n input format is wrong, please try again ... ') Continue break if a = = ' Y ' or a = = ' Y ': continue else:breakif balance = = Salary:print (' \ n trading ended, purchase failed ... ') exit () Else:print (' \ n you have successfully Purchase the following items: \ n ') for item in Shopping_list: Print (item) print (' \ n total consumption%d, balance%d yuan '% (salary-balance, balance)) exit ()