1: 条件判断语句
- if 条件判断 语句
if 条件一: 执行1 elif 条件二: 执行2 elif 条件三: 执行3 else 执行4
- 写一个简单的猜拳游戏
# 导入模块 import random # 调用模块中的一个函数 computer = random.randint(0,2) print(computer) player = int(intput('请输入:0表示石头,1表示剪刀,2表示布')) if((computer== 0) and (player == 2) or (computer == 1) and (player == 0) or (computer == 2) and (player == 1)): print('player success') elif(computer == player): print('The two sides draw !') else print('Computer success !')
2: 三目运算符 或 三元运算符 或 三目表达式
c = a if a > b else b
if a > b : c = a
else : c = b
3: 循环
(1): while 1 : 循环语句的关键字
while 1 : exec function1
(2): for temp_vary(临时变量) in sequence(序列)
- break: 终止此循环
- continue : 结束当前循环,继续执行下一次循环