while循环语句
ha = 3
while ha == 2 : #表达式永远为true
nu = float(input("立即键入一个数字哦:"))
print("你输入的东西是:",nu)
print("结束")
for循环
for i in [0,1,2]:
print(i)
0
1
2
for i in range(1,4):
print(i)
1
2
3
while循环案例——计算1~1000偶数和
i = 0
sum = 0
while i < 101:
if i % 2 == 0:
sum += i
i += 1
print("1到100之间偶数和为:%s"%sum)
print("1到100之间偶数和为:",sum)
1到100之间偶数和为:2550
1到100之间偶数和为: 2550
while嵌套案例
i = 1
while i < 6:
j = 0
while j < i:
print("*",end="")
j += 1
print("\n")
i += 1
*
**
***
****
*****
for语句中的其他语句
break语句
for i in range(8):
print("------")
if i == 3:
break
print(i)
------
0
------
1
------
2
------
continue语句
for i in range(8):
i += 1 #从1开始
print("------")
if i == 5:
continue #结束上面的5,从下一个开始
print(i)
------
1
------
2
------
3
------
4
------
------
6
------
7
------
8
pass语句
for letter in 'Runoob':
if letter == "o":
pass
print('执行pass模块')
print('当前字母:',letter)
print("bye!")
当前字母: R
当前字母: u
当前字母: n
执行pass模块
当前字母: o
执行pass模块
当前字母: o
当前字母: b
bye!
else语句
count = 0
while count < 5:
print(count,"is less than 5")
count += 2
else:
print(count,"is not less than 5")
0 is less than 5
2 is less than 5
4 is less than 5
6 is not less than 5