Small exercises for lists and loops used in Python

Source: Internet
Author: User

#author devilfproduct_list= [    ('iphone',5800),    ('Mac Pro',9800),    ('Bike', -),    ('Watch',10000),    ('Coffee',123)]shop_list=[]salary= Input ('pls enter your salary:')ifsalary.isdigit (): #判断是否为整形, or exit salary=int(Salary) whileTrue: forIndex,infoinchEnumerate (product_list): #打印列表 # Print (Product_list.index (info), info) print (in Dex,info) User_choice= Input ('pls enter what buy\n>>>:') #输入要购买的商品 numberifuser_choice.isdigit (): #再判断下输入的是否是数字, otherwise quit User_choice=int(User_choice)ifUser_choice <= Len (product_list) and User_choice >=0: #判断输入的编号是否在商品列表的编号范围内 p_item=Product_list[user_choice] #取出编号对应的商品名称ifp_item[1] <=Salary: #判断改商品的价格是否在工资范围内 shop_list.append (P_item) #如果在工资范围内, buy! Salary-= p_item[1] #总工资便会减少对应的钱 Print ('Added%s to shopping cart,your current balance is%s'%(p_item,salary))Else: Print ('your current balance was%s, now was not enough'%(Salary))Else: Print ('Product%s is not exist!'%user_choice) elif User_choice=='Q': #如果输入的是'Q', you exit print ('-------------Shopping list------------')             forPinchshop_list:print (p) print ('Quit.....,your Current Money:', Salary) exit ()Else: Print ('Invalid Option')

Small exercises for lists and loops used in Python

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.