age = 22 #优化前 ''' for i in range(10): guess_num = int(input('input your guess num:')) if guess_num == age : print('Congratulations ! you got it.') elif guess_num > age: print('Think smaller!') else: print('Think Big...') ''' #优化后:要求输入用户名输入正确以后不执行语句 ''' for i in range(10): if i < 3: guess_num = int(input('input your guess num:')) if guess_num == age : print('Congratulations ! you got it.') break #跳出整个循环 elif guess_num > age: print('Think smaller!') else: print('Think Big...') else: print('too many attumt.bye') break ''' #再优化:要求循环3次后询问一次 counter = 0 for i in range(10): print('counter',counter) if counter < 3: guess_num = int(input('input your guess num:')) if guess_num == age : print('Congratulations ! you got it.') break #跳出整个循环 elif guess_num > age: print('Think smaller!') else: print('Think Big...') else: continue_confirm = input('Do you want to continue because you are stupid:') if continue_confirm == 'y': counter = 0 continue #跳出本次循环 else: print('bye') break counter += 1 #counter = counter + 1
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。