step:
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