Python Learning: Shopping Cart Program

Source: Internet
Author: User

Shopping Cart Procedure

product_list = [
(' Mac ', 9000),
(' Kindle ',
(' Tesla ', 900000),
(' Python book ',
(' bike ', '),
]
saving = input (' Please input your money: ')
Shopping_car = []
if Saving.isdigit (): #验证输入的是否为数字格式, if established then continue
saving = Int (saving)
While True:
#打印商品内容
# for I in Product_list:
# Print (Product_list.index (i), i)
# for I in Enumerate (product_list,1):
# Print (i)
For i,v in Enumerate (product_list,1):
print (i,v) #print (i, ' >>>> ', v)
choice = input (' Select purchase item number [exit: Q]: ') #引导用户选择商品
if Choice.isdigit ():
choice = Int (choice) #验证输入是否合法
#len (product_list) Get the list length
If choice > 0 and Choice <= len (product_list):
P_item = product_list[choice-1] #将用户选择商品通过choice取出来
if p_item[1] < saving:
Saving-= p_item[1]
shopping_car.append (P_item) #如果钱够, subtract the price of the commodity with the saving principal and add the item to the shopping cart
Else:
print (' Insufficient balance, remaining%s '%saving)
print (P_item)
Else:
print (' Code not present ')
elif Choice = = ' Q ':
print ('--------------you and purchase the following items--------------')
For i in Shopping_car:
print (i) #循环遍历购物车内的商品, the shopping cart is stocked with purchased goods
print (' You still have%s dollar money '%saving)
Break
Else:
print (' Invalid input ')

Python Learning: Shopping Cart Program

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.