一、if判断
if 条件:
else:
age = 12inputAge = int(input("input your guess Age"))if age==inputAge: print("inputAge is right")else: print("inputAge is wrong")
运行,并在控制台输入值,查看结果
1.输入11
2.输入12
二、多分支if判断
if 条件:
elif 条件:
else
age = 12inputAge = int(input("input your guess Age"))if age==inputAge: print("inputAge is right")elif age
运行,输入22,查看结果
三,缩进介绍
执行以下代码,与上面的代码唯一的区别在于print()没有缩进
age = 12inputAge = int(input("input your guess Age"))if age==inputAge:print("inputAge is right")elif age
执行,查看运行结果
查看控制台,缩进异常。
原因:python是通过缩进区分执行的代码块儿的,不像java那样,通过{}来区分,
1官方规定缩进4个空格,同时不建议用Tab键代替空格键,原因在于linux和win上面的Tab不一样
2.缩进空格数随意,只要保持代码块的缩进空格数一致就行
3.为方便开发,将tab键改为4个空格,python3-pycharm TAB键转换为4个空格
java的 if判断
python 的if判断语法不同于java,没有{},条件也不需要用()括起来
1.if 判断
if (条件) {
}else{
}
public static void main(String[] args) { System.out.print("input your guess Age"); Scanner input = new Scanner(System.in); int age = 12; int inputAge = input.nextInt(); if (age==inputAge){ System.out.print("inputAge is right"); }else { System.out.print("inputAge is wrong"); } }
运行结果,输入12
2.多分支if判断
if(条件){
}else if(条件){
}else{
}
public static void main(String[] args) { System.out.print("input your guess Age"); Scanner input = new Scanner(System.in); int age = 12; int inputAge = input.nextInt(); if (age==inputAge){ System.out.print("inputAge is right"); }else if (age
运行,输入22,查看结果