1 age = 22 2 #优化前 3 ''' 4 for i in range(10): 5 guess_num = int(input('input your guess num:')) 6 if guess_num == age : 7 print('Congratulations ! you got it.') 8 elif guess_num > age: 9 print('Think smaller!') 10 else: 11 print('Think Big...') 12 ''' 13 #优化后:要求输入用户名输入正确以后不执行语句 14 ''' 15 for i in range(10): 16 if i < 3: 17 guess_num = int(input('input your guess num:')) 18 if guess_num == age : 19 print('Congratulations ! you got it.') 20 break #跳出整个循环 21 elif guess_num > age: 22 print('Think smaller!') 23 else: 24 print('Think Big...') 25 else: 26 print('too many attumt.bye') 27 break 28 ''' 29 #再优化:要求循环3次后询问一次 30 counter = 0 31 for i in range(10): 32 print('counter',counter) 33 if counter < 3: 34 guess_num = int(input('input your guess num:')) 35 if guess_num == age : 36 print('Congratulations ! you got it.') 37 break #跳出整个循环 38 elif guess_num > age: 39 print('Think smaller!') 40 else: 41 print('Think Big...') 42 else: 43 continue_confirm = input('Do you want to continue because you are stupid:') 44 if continue_confirm == 'y': 45 counter = 0 46 continue #跳出本次循环 47 else: 48 print('bye') 49 break 50 counter += 1 #counter = counter + 1