while循环 它的一般形式为:
while(条件)
语句:
print( )
while循环表示当条件为真时,便执行语句。直到条件为假才结束循环。并继续执行循环程序外的后续语句。
例如 :x=100 先输入起始值
y=2000 再输入终止值 而且 起始值不能比终止值大。
while x<500:
if x<y:
print(x)
y+=x
x+=1
print(y)
x+=1 相当于 x=x+1
随机石头剪刀布游戏 无限死循环代码:
import random
while True:
pc = random.randint(1,3)#电脑
praye = int(input("请输入1、石头 2、剪刀 3、布 "))
if praye > 0 and praye < 4:
if(praye == 1 and pc == 2 or praye == 2 and pc == 3 or praye ==3 and pc == 1):
print("爸爸赢 ")
elif (praye == pc):
print("平局")
else:
print("儿子赢 ")
else:
print("输入有误")
break某一条件满足时,退出循环,不再执行后续重复的代码
continue某一条件满足时,不执行后续重复的代码 结束当次循环,#里面执行下一次循环。
网友评论