1、while循环
当条件为真时,就会一直执行语句1
'''while 条件:
语句1
'''
#当条件为真时,就会一直执行语句1
'while 条件:\n 语句1\n'
1.1、输入1到5得数(包含)
number = 1
while number<=5:
print(number)
number+=1
1
2
3
4
5
1.2、输入1到10得偶数
number = 1
while number<=5:
if number %2 == 0:
print(number)
number+=1
2
4
1.3、设置标志
- flag
flag = True
while flag:
stu_grade1 = float(input("学生成绩(输入-1结束):>>"))
if stu_grade1 == -1:
flag = False
elif stu_grade1 >= 90:
print("A")
elif stu_grade1 >= 80:
print("B")
elif stu_grade1 >= 60:
print("C")
else:
print("D")
print("已退出!")
学生成绩(输入-1结束):>>90
A
学生成绩(输入-1结束):>>80
B
学生成绩(输入-1结束):>>-1
已退出!
1.4、beak语句(跳出)
while True:
stu_grade1 = float(input("学生成绩(输入-1结束):>>"))
if stu_grade1 == -1:
print("已退出!")
break
elif stu_grade1 >= 90:
print("A")
elif stu_grade1 >= 80:
print("B")
elif stu_grade1 >= 60:
print("C")
else:
print("D")
学生成绩(输入-1结束):>>90
A
学生成绩(输入-1结束):>>80
B
学生成绩(输入-1结束):>>-1
已退出!
1.5、continue
- 下在面的例子中,当语句满足number==2时,执行continue语句,直接回到while,而不是执行下面的print
- 结束当次循环,执行下次循环
number = 1
while number <5:
number +=1
if number == 2:
continue
print(number)
3
4
5
1.6、while…else 语句
- 当while正常执行完后,会执行else里的语句
'''
while 条件:
。。。
else:
。。。
'''
number = 1
while number <5:
number +=1
if number == 2:
continue
print(number)
else:
print("end")
3
4
5
end
2、print(补充)
- end的使用
- end=“以什么结尾”
- print() <=> print(end="\n")
print("Hellow python")
print("Hellow python")
print("Hellow python")
Hellow python
Hellow python
Hellow python
print("Hellow python",end="+")
print("Hellow python",end="\n")
print("Hellow python",end="_")
Hellow python+Hellow python
Hellow python_
3、九九乘法表(while)
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
num1 = 1
while num1<=9:
num2 = 1
while num2<=num1:
print(str(num1)+'×'+str(num2)+'='+str(num1*num2),end="\t")
num2 += 1
print()
num1 += 1
1×1=1
2×1=2 2×2=4
3×1=3 3×2=6 3×3=9
4×1=4 4×2=8 4×3=12 4×4=16
5×1=5 5×2=10 5×3=15 5×4=20 5×5=25
6×1=6 6×2=12 6×3=18 6×4=24 6×5=30 6×6=36
7×1=7 7×2=14 7×3=21 7×4=28 7×5=35 7×6=42 7×7=49
8×1=8 8×2=16 8×3=24 8×4=32 8×5=40 8×6=48 8×7=56 8×8=64
9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81
first = 1
while first <= 9:
sec = 1
while sec <= first:
print(str(sec) + "×" + str(first) + "=" + str(sec * first), end="\t")
sec += 1
print()
first += 1
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
- 网上大佬用一行代码实现乘法表
print ("\n".join("\t".join(["%s×%s=%s" %(x,y,x*y) for y in range(1, x+1)]) for x in range(1, 10)))
1×1=1
2×1=2 2×2=4
3×1=3 3×2=6 3×3=9
4×1=4 4×2=8 4×3=12 4×4=16
5×1=5 5×2=10 5×3=15 5×4=20 5×5=25
6×1=6 6×2=12 6×3=18 6×4=24 6×5=30 6×6=36
7×1=7 7×2=14 7×3=21 7×4=28 7×5=35 7×6=42 7×7=49
8×1=8 8×2=16 8×3=24 8×4=32 8×5=40 8×6=48 8×7=56 8×8=64
9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81
本文深入探讨了Python中的while循环,包括输入1到5的数、输入1到10的偶数、设置标志、使用break和continue语句、while...else结构。此外,还补充了print函数的end参数用法,并展示了如何用while循环实现九九乘法表。最后,提到了一行代码实现乘法表的高级技巧。
1万+

被折叠的 条评论
为什么被折叠?



