一、if判断
if 条件:
else:
age = 12
inputAge = 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 = 12
inputAge = int(input("input your guess Age"))
if age==inputAge:
print("inputAge is right")
elif age<inputAge:
print("inputAge is old")
else:
print("inputAge is young")
运行,输入22,查看结果
编辑
三,缩进介绍
执行以下代码,与上面的代码唯一的区别在于print()没有缩进
age = 12
inputAge = int(input("input your guess Age"))
if age==inputAge:
print("inputAge is right")
elif age<inputAge:
print("inputAge is old")
else:
print("inputAge is young")
执行,查看运行结果
编辑
查看控制台,缩进异常。
原因: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<inputAge){
System.out.print("inputAge is old");
}else {
System.out.print("inputAge is young");
}
}
运行,输入22,查看结果
编辑