print("我今年%d岁了,身高%米" %(age,height))
if控制if下面带缩进的代码块
例1: if (age>18):
print("aa")
print("bb")
运行以上代码会输出aabb
if和elif的例子
holiday_name = input(“请输入今天的节日”)
if (holiday_name) == “情人节”:
print(“买玫瑰”)
print(“看电影”)
elif (holiday_name) == “平安夜”:
print(“买苹果”)
print(“吃大餐”)
elif (holiday_name) = “生日”
print(“买蛋糕”)
else
print(“每天都是节日啊”)
多个if嵌套,实现多层筛选
if(height>180):
if(money>999999):
print("我可以!")
else:
print("太穷了")
else:
print("太矮了")
只有当两层if都成立才打印"我可以"
大的if配合大的else,小if配合小else
例2:剪刀石头布
石头=1,剪刀=2,布=3
player出一个数,computer出一个数,列出所有取胜的可能
if (palyer == 1 and computer== 2) or (player==2 and computer==3) or (player == 3 and computer == 1):
print("你赢了")
elif (palyer == 1 and computer== 1) or (player==2 and computer==2) or (player == 3 and computer == 3):
print("平局")
else:
print("你输了")
import random
computer =random.randint(1,3) # computer会从1到3随机取一个数字
来源:https://www.cnblogs.com/chrr/p/12364153.html