Python implements the idea and code of shopping programs, and python shopping program ideas

Source: Internet
Author: User
Tags dell xps

Python implements the idea and code of shopping programs, and python shopping program ideas

Requirements:

After the program is started, ask the user to enter the salary, and then print the commodity list with the serial number
The user enters the product serial number to purchase the corresponding product, or enters 'q' to exit the purchase interface.
After selecting the item, check whether the balance is sufficient. If the balance is sufficient, deduct the fee directly. If the balance is insufficient, the system prompts that the balance is insufficient.
After a user buys a product, or enters 'q' to exit the purchase page, the user prompts: Continue to purchase? (Y/N) for multiple purchases
If the user has purchased the product, print out the list of the purchased product, total amount, and balance. if the user has not bought any product, print: the transaction ends and the purchase fails.
Readme:

Run the program, enter the salary, and select the purchased item based on the serial number of the product list. You can choose to purchase multiple times or not to purchase

Flowchart:


Code:

# Simple Shopping applet product_list = [['surface pro 4', 7800], ['Dell xps 15', 12000], ['macbook ', 12000], ['xiaomi 6', 2499], ['iphone7', 4600], ['Nut Pro ', 1499] shopping_list = [] # judge whether the input salary format is correct while True: salary = input ('\ n Please enter your salary:') if not salary. isdigit (): # The salary is not a number, and the print ('\ n input format is incorrect! Please input again... ') continue breakbalance = salary = int (salary) print (' \ n ----------- welcome to purchase ---------- \ n') # generate a list of products with serial numbers for index, item in enumerate (product_list): print (index, item) # determine whether the input Serial number meets the requirements while True: I = input ('\ n enter the serial number of the product you want to purchase, or enter q to cancel the purchase: ') if I = 'q': # Enter q to exit the purchase page while True: a = input (' \ n, continue purchasing? (Y/N): ') if! = 'N' and! = 'N' and! = 'Y' and! = 'Y': print ('\ n input format is incorrect. Please try again... ') continue elif a = 'y' or a = 'y': # continue to buy break else: # if balance = salary: # print ('\ n transaction ended, purchase failed... ') exit () else: # settlement print (' \ n you have successfully purchased the following items: \ n') for item in shopping_list: print (item) print ('\ n total consumption amount % d yuan, balance % d yuan' % (salary-balance, balance) exit () continue if not I. isdigit (): # The serial number is not a number. print ('\ n input format is incorrect! Please input again... ') continue I = int (I) if I <0 or I> = len (product_list): # the sequence number range is incorrect. print (' \ n this product does not exist, please input again... ') continue break product = product_list [I] price = int (product [1]) # determine whether the balance is sufficient. if the balance is sufficient, deduct the fee directly. if the price is not enough, <= balance: balance-= price shopping_list.append (product_list [I]) print ('\ n you have successfully purchased % s, the current balance is % d yuan' % (product, balance) else: print ('\ n purchase failed, your balance is insufficient... ') while True: a = input (' \ n continue to purchase? (Y/N): ') if! = 'N' and! = 'N' and! = 'Y' and! = 'Y': print ('\ n input format is incorrect. Please try again... ') continue break if a = 'y' or a = 'y': continue else: breakif balance = salary: print (' \ n transaction ends, purchasing failed... ') exit () else: print (' \ n you have successfully purchased the following items: \ n') for item in shopping_list: print (item) print ('\ n total consumption amount % d yuan, balance % d yuan' % (salary-balance, balance) exit ()

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.