一、break
在while循环中如果想要提前结束则需要用bleak语句
如打印1-100中的数字:
n=1
while True:
print(n)
n=n+1
if n>100:
break
二、continue
continue是跳过本轮循环,直接执行下一次循环的语句
如打印1-100中的奇数:
n=1
while True:
if n%2==0:
continue
print(n)
n=n+1
if n>100:
break
三、pass
pass的作用是占位
如:
while True:
会报错
而
while True:
pass
不会报错
最后
送大家一个小礼物,复制以下代码到python,有惊喜!
注:pyautogui库自行安装
import turtle
import tcwbag.help
import pyautogui
turtle.setup(1575,960)
turtle.tracer(0)
while True:
tcwbag.help.randomsize(1)
x, y = pyautogui.position()
xing=1575//2+10
ying=960//2-10
x=x-xing
y = 0-y+ying
if (x>0 and y>0):
turtle.pencolor('red')
elif (x<0 and y>0):
turtle.pencolor('blue')
elif (x>0 and y<0):
turtle.pencolor('cyan')
else:
turtle.pencolor('black')
turtle.goto(x, y)
turtle.update()
turtle.done()