while简单循环
i = 1while i <= 5: print('啦啦啦啦') i += 1
和C语言也一样形式。同样的思路,i加1后又回到while判断。如果没有i += 1,程序将会死循环。
对于猜拳游戏,while:
import randomwhile True:player = int(input('请出拳: 0--石头;1--剪刀;2--布: '))computer = random.randint(0, 2)if((player == 0) and (computer == 1) or (player == 1) and (computer ==2) or (player == 2) and (computer == 0)): print('玩家获胜')elif player == computer: print('平局')else: print('电脑获胜')
while嵌套
假设操场跑五圈,一圈做三个俯卧撑
i = 0while i < 5: j = 0 print('操场跑圈中。。。。。。') while j < 3: print('做一个俯卧撑') j += 1 i += 1
补一下if elif结构
if后一个条件,elif后1不成立,判断第二个条件,else后前两个都不行,执行else后条件
if else结构
if后如=如果不成立,执行else后的条件