1.while 条件:
如果条件为真(True)执行这里的语句
>>> counts = 3
>>> while counts > 0:
print("小雨晴想吃火锅")
counts = counts - 1
小雨晴想吃火锅
小雨晴想吃火锅
小雨晴想吃火锅
2.counts = counts + 1与最外层中的else中的语句并列时,那么用户输入任何数字都会循环三次;counts = counts + 1多一个缩进,那么用户只有输入小于3的数字才会循环三次。
"""用Python设计的第一个游戏改进版"""
counts =3
while counts > 0:
temp = input("不妨猜一下小雨晴现在心里想的是哪个数字:")
guess= int(temp)
if guess == 3:
print("你是小雨晴心里的蛔虫嘛?!")
print("哼,猜中了也没奖励!")
else:
if guess > 3:
print("大啦~")
else:
print("小啦~")
counts = counts - 1
print("游戏结束,不玩啦^_^")
"""用Python设计的第一个游戏改进版"""
counts =3
while counts > 0:
temp = input("不妨猜一下小雨晴现在心里想的是哪个数字:")
guess= int(temp)
if guess == 3:
print("你是小雨晴心里的蛔虫嘛?!")
print("哼,猜中了也没奖励!")
else:
if guess > 3:
print("大啦~")
else:
print("小啦~")
counts = counts - 1
print("游戏结束,不玩啦^_^")
3.break语句:跳出一层循环体。
>>> counts = 3
>>> while counts > 0:
print("小雨晴想吃火锅")
counts = counts - 1
break
小雨晴想吃火锅
"""用Python设计的第一个游戏改进版"""
counts =3
while counts > 0:
temp = input("不妨猜一下小雨晴现在心里想的是哪个数字:")
guess= int(temp)
if guess == 3:
print("你是小雨晴心里的蛔虫嘛?!")
print("哼,猜中了也没奖励!")
break
else:
if guess > 3:
print("大啦~")
else:
print("小啦~")
counts = counts - 1
print("游戏结束,不玩啦^_^")