python-Use python to write a small shopping program

step:

  1. Print product content
  2. Guide users to choose products
  3. Verify that the input is legal
  4. Take out the product selected by the user through choice
  5. If the money is enough, subtract the price of the product from the principal saving
  6. Add the product to the shopping cart
  7. Cycle through the items in the shopping cart, the shopping cart stores the purchased items
product_list=[('Mac computer',9500),('windows computer',800),('Ferrari',8800000),('Python introductory tutorial',100),('Huawei',6000),]
saving=input('please input your money:')
shopping_car=[]if saving.isdigit():
 saving=int(saving)while True:
  # Print product content
  for i,v inenumerate(product_list,1):print(i,'>>>>',v)
 
   # Guide users to choose products
  choice=input('Select purchase product number[Exit: q]:')
 
  # Verify that the input is legal
  if choice.isdigit():
   choice=int(choice)if choice>0 and choice<=len(product_list):
    # Take out the product selected by the user through choice
    p_item=product_list[choice-1]
 
    # If the money is enough, subtract the price of the product from the principal saving and add the product to the shopping cart
    if p_item[1]<saving:
     saving-=p_item[1]
 
     shopping_car.append(p_item)else:print('Insufficient balance, left%s'%saving)print(p_item)else:print('Encoding does not exist')
  elif choice=='q':print('------------You have purchased the following products----------------')
   # Cycle through the items in the shopping cart, the shopping cart stores the purchased items
   for i in shopping_car:print(i)print('You have left%s yuan'%saving)breakelse:print('invalid input')

Run output:

please input your money:100000001>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1('Mac computer',9500)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1('Mac computer',9500)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1('Mac computer',9500)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:2('windows computer',800)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:3('Ferrari',8800000)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]: 3 insufficient balance, 1170700 left('Ferrari',8800000)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1('Mac computer',9500)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:4('Python introductory tutorial',100)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:5('Huawei',6000)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:6
Encoding does not exist
1>>>>(' Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:7
Encoding does not exist
1>>>>(' Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1('Mac computer',9500)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:1*4
invalid input
1>>>>(' Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]: 3 insufficient balance, 1145600 left('Ferrari',8800000)1>>>>('Mac computer',9500)2>>>>('windows computer',800)3>>>>('Ferrari',8800000)4>>>>('Python introductory tutorial',100)5>>>>('Huawei',6000)
Select purchase product number[Exit: q]:q
- - - - - - - - - - - - You have purchased the following products----------------('Mac computer',9500)('Mac computer',9500)('Mac computer',9500)('windows computer',800)('Ferrari',8800000)('Mac computer',9500)('Python introductory tutorial',100)('Huawei',6000)('Mac computer',9500)
You have 1145600 yuan left
 
Process finished with exit code 0

Recommended Posts

python-Use python to write a small shopping program
How to write a confession program in python
Use C++ to write Python3 extensions
How to write python configuration file
How to write classes in python
How to write return in python
A practical guide to Python file handling
How to make a globe with Python
A complete guide to Python web development
How to write win programs in python
How to write try statement in python
Python class decorator, use a small demo
How to debug python program using repr
How to sort a dictionary in python
How to program based on interfaces in Python
Python3 development environment to build a detailed tutorial
How to simulate gravity in a Python game
Teach you how to write games in python
How to read and write files with Python
01. Introduction to Python
100 small Python examples
Python write Tetris
Introduction to Python
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop