1 #!-*- coding:utf-8 -*- 2 # Author:Steve 3 book_list=[('三国演义',60), 4 ('水浒传',65), 5 ('红楼梦',55), 6 ('西游记',70), 7 ('山海经',80), 8 ('封神演义',55)] 9 10 shopping_list=[] 11 12 times_of_input=5 13 14 user_balance=input('请输入您的余额(共有\033[31;1m%d\033[0m次机会):'%(times_of_input)) 15 16 for times in range(5): 17 18 if user_balance.isdigit(): 19 user_balance=int(user_balance) 20 print('\033[35;1m欢迎光临,黑心书店!!!\033[0m') 21 22 for index,items in enumerate(book_list): 23 print(index,items) 24 25 while True: 26 user_choice=input('请问您想购买哪本书?\n') 27 length_of_booklist=len(book_list) 28 29 for i in range(length_of_booklist): 30 31 if user_choice==book_list[i][0]: 32 33 if user_balance>=book_list[i][1]: 34 shopping_list.append(book_list[i]) 35 user_balance-=book_list[i][1] 36 print('已经将%s加入到您的购物表,您当前的余额为:\033[31;1m%s\033[0m元'%(book_list[i][0],user_balance)) 37 else: 38 print("\033[41;1m您的余额为:\033[0m\033[31;1m%s\033[0m元.\033[41;1m余额已不足\033[0m" % user_balance) 39 40 user_choice2=input('请问您是否还需要其他的书? 是/否\n') 41 42 if user_choice2=='是': 43 break 44 elif user_choice2=='否': 45 print("--------购物表-------") 46 cost = 0 47 for p in shopping_list: 48 print(p) 49 cost += p[1] 50 print('\n您花费了:\033[31;1m%s\033[0m元' % cost) 51 print('\n您当前余额为:\033[32;1m%s\033[0m元' % user_balance) 52 print('\n\033[33;1m欢迎下次再来黑心书店!!!\033[0m') 53 exit() 54 else: 55 print('你怕是来捣乱的吧OAO!') 56 exit() 57 58 else: 59 user_choice3=input('没有您想要的书,请问是否再次进行选择? 是/否\n') 60 61 if user_choice3=='否': 62 print("--------购物表-------") 63 cost = 0 64 for p in shopping_list: 65 print(p) 66 cost += p[1] 67 print('\n您花费了:\033[31;1m%s\033[0m元' % cost) 68 print('\n您当前余额为:\033[32;1m%s\033[0m元' % user_balance) 69 print('\n\033[33;1m欢迎下次再来黑心书店!!!\033[0m') 70 exit() 71 elif user_choice3=='是': 72 continue 73 else: 74 print('你怕是来捣乱的吧OAO!') 75 exit() 76 77 78 else: 79 times_of_input-=1 80 81 if times_of_input!=0: 82 print('请输入阿拉伯数字!谢谢') 83 user_balance = input('请再次输入您的余额(您还有\033[31;1m%d\033[0m次机会):'%(times_of_input)) 84 85 else: 86 print('可能您是个穷光蛋吧!再见。。。')

浙公网安备 33010602011771号