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
for循环语句示例应用
原创文章标签 for循环语句示例应用 文章分类 后端开发
上一篇:python开发规则
下一篇:if语句
-
Python的控制语句和循环语句
if、for、while语句
循环语句 控制语句 -
SqlServer中循环和条件语句示例!
-- ╔════════╗ -- ===================
sqlserver 循环 条件 while if -
【Python】循环语句 ② ( while 嵌套循环 | 代码示例 - while 嵌套循环 )
一、while 嵌套循环1、while 嵌套循环语法2、代码示例 - while 嵌套循环
开发语言 Python 嵌套循环 PyCharm 控制变量 -
python 循环语句应用举例
python 循环语句应用举例
for循环 while循环 Python -
循环语句综合应用
合数分解#!/bin/sh#把一个大于等于2以上的正整数,分解成质因子的连乘积
shell i++ 质因子 ide 编程语言