if 语句
Python中if语句的一般形式如下所示:
if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3
- 如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句
- 如果 "condition_1" 为False,将判断 "condition_2"
- 如果"condition_2" 为 True 将执行 "statement_block_2" 块语句
- 如果 "condition_2" 为False,将执行"statement_block_3"块语句
Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else。
注意:
- 1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
- 2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
- 3、在Python中没有switch – case语句
print("=======欢迎进入狗狗年龄对比系统========")
control = "N"
while control=="N":
try:
age = int(input("请输入您家狗的年龄:"))
#print(" ")
age = float(age)
if age < 0:
print("您在逗我?")
elif age == 1:
print("相当于人类14岁")
#break
elif age == 2:
print("相当于人类22岁")
#break
else:
human = 22 + (age - 2)*5
print("相当于人类:",human)
#break
except ValueError:
print("输入不合法,请输入有效年龄")
print("")
control = input("退出(Y/N)?")
print("")
###退出提示
input("点击 enter 键退出")