for循环语句示例应用
发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,age = 22#优化前'''for i in range(10): guess_num = int(input('input your guess num:')) if guess_n
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 = 0for 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