if 条件:
同缩进的代码块
if age > 18:
print("")
if age > 30:
print("")
elif age > 18:
print("")
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
1.所有分支都可以根据需求决定是否有else结构,else结构就是条件(所有条件)不满足才去执行的分支
2.elif可以根据需求出现多次(代码层次方面要考虑条件的先后顺序)
if嵌套
内部条件判断与否决定于外层条件
if嵌套
内部条件判断与否决定于外层条件
if 条件:
if 条件:
pass
elif 条件:
pass
else:
Pass
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
else:
# 条件不成立时执行的子代码块
代码1
代码2
代码3
sex = ‘male‘
age = ‘22‘
is_beautiful = True
if sex == ‘male‘ and age == ‘22‘ and is_beautiful:
print("开始表白...")
else:
print(‘你好‘)
print(‘other code1...‘)
print(‘other code2...‘)
print(‘other code3...‘)
示例:
如果成绩 >= 90,那么:优秀
如果成绩 >= 80且 < 90, 那么:良好
如果成绩 >= 70且 < 80, 那么:普通
其他情况:很差
score = input(‘please input your score: ‘) # score=‘100‘
score = int(score)
if score >= 90:
print(‘优秀‘)
elif score >= 80:
print(‘良好‘)
elif score >= 70:
print(‘普通‘)
else:
print(‘很差‘)
elif 条件:
在任何结构(缩进下,需要条件判断)就可以出现if分支结构
猜数字
age_of_you=26
guess=int(input(">>:"))
if guess>age_of_you:
print("输的太大了,再试试")
elif guess<age_of_you:
print("输的太小了")
else:
print("恭喜你,猜对了")
语法:
while 条件:
循环体
1.当条件满足时,会执行循环体,然后再去判断条件成功与否
2.如果还满足,重复1,2的过程
3.如果不满足,结束循环体
注:如果需要结束循环,一定需要让条件在某种情况下不满足
语法:
while 条件:
代码1
代码2
代码3
while True:
name=input(‘please input your name: ‘)
pwd=input(‘please input your password: ‘)
if name == ‘egon‘ and pwd == ‘123‘:
print(‘login successful‘)
else:
print(‘username or password error‘)
重点
break:结束所属循环(本层)
while True:
name=input(‘please input your name: ‘)
pwd=input(‘please input your password: ‘)
if name == ‘egon‘ and pwd == ‘123‘:
print(‘login successful‘)
break
else:
print(‘username or password error‘)
print(‘===>>>>>‘)
print(‘===>>>>>‘)
continue:结束所属循环的本次循环,进入下一次循环
示例一
count=1
while count < 6: #count=6
if count == 4:
count += 1
continue
print(count)
count+=1
示例二:
while True:
name=input(‘please input your name: ‘)
pwd=input(‘please input your password: ‘)
if name == ‘egon‘ and pwd == ‘123‘:
print(‘login successful‘)
break
else:
print(‘username or password error‘)
# continue # 此处加continue无用
循环嵌套
如果有循环写在了循环内部,就形成了循环嵌套
1.死循环:不能主动结束的循环,想要结束只能通过break
2.0,"",[],{},None可以被while 及 if这种需要条件的关键词转换为False,其他都是True
死循环
例子
while info: # 如果循环是死循环,想要结束循环,只能借助break
cmd = input(‘cmd: ‘)
if cmd == ‘Q‘ or cmd == ‘q‘:
break
print(cmd)
count = 0
while count < 3:
usr = input(‘usr: ‘)
pwd = input(‘pwd: ‘)
if usr == ‘owen‘ and pwd == ‘123‘:
print(‘登陆成功,进入交互界面‘)# while True:
cmd = input(‘cmd: ‘)
if cmd == ‘Q‘ or cmd == ‘q‘:
break # 结束所属循环 while True
print(cmd, ‘指令‘)
break # 结束所属循环 while count < 3
else:
print(‘登陆失败‘)
count += 1
while...else...: else分支会在while循环正常结束(没有被break结束)时执行
c = 0
while c < 10:
c += 1
if c == 5:
continue # 1~10没有5
break # 1~4
print(c)
else:
print(‘我被执行了‘)
for用来完成特定的循环需求
语法:
for 循环得到的结果 in 被循环的容器:
循环体
1.第一次循环,循环得到的结果 就是 被循环的容器 中第一个值
2.再次循环,循环得到的结果 会被重新赋值为 被循环的容器 中第二个值,以此类推
注:每一次循环 循环得到的结果 不使用,下一次循环就会被覆盖,无法找回
For v in [1.2.3.4.]
print(v)
dic = {‘a’:1.’b’:2.’c’:3}
print(dic)
Range()函数:可以帮助建立一个容器
for v in range(4):
print(v)
==>[0.1.2.3]
for v in range(1,6)
print(v)
==>[1.2.3.4.5]
for v in range(1.6.2)
print(v)
==>[1.3.5]
for循环
一般不考虑死循环
Break也能主动结束for循环
Countinue也能主动结束for的本次循环
例:99乘法表
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
for i in range(1, 10):
# i = 1 => x1 x2 x3 ... x9
# i = 2 => x2 x3 ... x9
# print(‘>>>‘, i)
# 第一行就打印一条,所以循环次数只能有1次,随着行数增加,次数就增加
for j in range(1, i + 1):
res = ‘%sx%s=%s‘ % (j, i, i * j)
print(res, end=‘ ‘)
print()
原文:https://www.cnblogs.com/komorebi/p/10720018.html