python中对列表和循环使用的小练习
生活随笔
收集整理的這篇文章主要介紹了
python中对列表和循环使用的小练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#author devilfproduct_list = [('iphone',5800),('Mac Pro',9800),('Bike',800),('Watch',10000),('Coffee',123)
]
shop_list = []
salary = input('pls enter your salary: ')if salary.isdigit(): #判斷是否為整形,否則會退出salary = int(salary)while True:for index,info in enumerate(product_list): #打印列表
# print(product_list.index(info),info)print(index,info)user_choice = input('pls enter what you buy\n>>>: ') #輸入要購買的商品 編號if user_choice.isdigit(): #再判斷下輸入的是否是數字,否則退出user_choice = int(user_choice)if user_choice <= len(product_list) and user_choice >= 0: #判斷輸入的編號是否在商品列表的編號范圍內p_item = product_list[user_choice] #取出編號對應的商品名稱if p_item[1] <= salary: #判斷改商品的價格是否在工資范圍內shop_list.append(p_item) #如果在工資范圍內,購買!salary -= p_item[1] #總工資便會減少對應的錢print('Added %s into shopping cart,your current balance is %s' %(p_item,salary))else:print('your current balance is %s ,now is not enough' % (salary))else:print('product %s is not exist!' % user_choice )elif user_choice == 'q': #如果輸入的是 'q',便會退出print('-------------shopping list------------')for p in shop_list:print(p)print('quit.....,your current money: ',salary)exit()else:print('Invalid option')
?
轉載于:https://www.cnblogs.com/Devilf/p/8044713.html
總結
以上是生活随笔為你收集整理的python中对列表和循环使用的小练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu14.04下apt-get
- 下一篇: websocket python爬虫_p