Python to achieve shopping process ideas and code
- 2020-06-12 09:52:44
- OfStack
Requirements:
After launching the program, let the user enter a salary and print out a list of items with serial Numbers
The user enters the serial number of the product to purchase the corresponding product, or enters' q 'to exit the purchase interface
After selecting the goods, check whether the balance is enough, if enough, direct deduction, insufficient prompt insufficient balance
After each purchase, or enter 'q' to exit the purchase interface, the user will be prompted: Do you want to continue the purchase? (Y/N) to achieve multiple purchases
If the user purchases the goods, print out the list of purchased goods, total amount and balance; If the user does not buy any goods, print: transaction closed, shopping failed
Readme:
Run the program, enter your salary, and select the item you want to buy based on the serial number of the item list. You can choose multiple purchases or no purchases
Flow chart:
Code:
# Simple shopping applet
product_list = [
['surface pro 4', 7800],
['dell xps 15', 12000],
['macbook', 12000],
[' millet 6', 2499],
['iphone7', 4600],
[' nuts Pro', 1499]
]
shopping_list = []
# Determine if the salary format entered is correct
while True:
salary = input('\n Please enter your salary: ')
if not salary.isdigit(): # Salary is not a number. Close the loop
print('\n Incorrect input format! Please re-enter ...')
continue
break
balance = salary = int(salary)
print('\n----------- Welcome to buy ------------\n')
# Generates a list of items with serial Numbers
for index, item in enumerate(product_list):
print(index, item)
# Determine if the input serial number meets the requirements
while True:
while True:
i = input('\n Enter the item number you want to buy, or enter q Cancel the purchase: ')
if i == 'q': # The input q Exit the purchase interface
while True:
a = input('\n Do you want to keep buying? (Y/N) : ')
if a != 'n' and a != 'N' and a != 'y' and a != 'Y':
print('\n Incorrect input format. Please try again ...')
continue
elif a == 'y' or a == 'Y': # Continue to buy
break
else: # After buying
if balance == salary: # I didn't buy anything
print('\n The transaction ends and the purchase fails ...')
exit()
else: # settlement
print('\n You have successfully purchased the following products: \n')
for item in shopping_list:
print(item)
print('\n Total amount of consumption %d Yuan, balance %d yuan ' % (salary - balance, balance))
exit()
continue
if not i.isdigit(): # The serial number is not a number. End the loop
print('\n Incorrect input format! Please re-enter ...')
continue
i = int(i)
if i < 0 or i >= len(product_list): # Incorrect serial number range, end loop
print('\n This item does not exist, please re-enter ...')
continue
break
product = product_list[i]
price = int(product[1])
# Judge whether the balance is sufficient, enough direct deduction, not enough to remind
if price <= balance:
balance -= price
shopping_list.append(product_list[i])
print('\n You have made a successful purchase %s , the current balance is %d yuan ' %(product, balance))
else:
print('\n Purchase failed, your balance is insufficient ...')
while True:
a = input('\n Do you want to keep buying? (Y/N) : ')
if a != 'n' and a != 'N' and a != 'y' and a != 'Y':
print('\n Incorrect input format. Please try again ...')
continue
break
if a == 'Y' or a == 'y':
continue
else:
break
if balance == salary:
print('\n The transaction ends and the purchase fails ...')
exit()
else:
print('\n You have successfully purchased the following products: \n')
for item in shopping_list:
print(item)
print('\n Total amount of consumption %d Yuan, balance %d yuan ' %(salary-balance, balance))
exit()