1. 程式人生 > >老男孩培訓練習購買商品

老男孩培訓練習購買商品

python

#!/usr/bin/env python #-*-coding=utf-8 -*- #AUTHOR:duwentao product_list = [ ('Iphone',5000), ('book',10), ('watch',10600), ('bike',800), ] shopping_list=[] #判斷工資是否為數字 salary = input("請輸入你的工資:") if salary.isdigit(): salary = int(salary) while True: for item in product_list: print (product_list.index(item),item) user_choice = input('請輸入你要購買的商品號:') if user_choice.isdigit(): user_choice = int(user_choice) if user_choice >= 0 and user_choice < len(product_list): p_item = product_list[user_choice] if p_item[1] > salary: print('余額不足') else: shopping_list.append(item) salary -= p_item[1] print('你已經購買了%s,您的余額為%s' %(item,salary)) else: print('你購買的商品不存在') elif user_choice == 'q': print ('----shopping list----') for p in shopping_list: print(p) print('你的余額為',salary) exit() else: print ("輸入有誤,請重新編號") print ("正在退出") exit()


老男孩培訓練習購買商品