[英]I think my if else statement is not working
This is the Code这是代码
Age = int(input("Enter Your Age: "))
if (Age > 18):
print("Your are 18+")
pass
if (Age == 18):
print("Your are 18")
pass
else:
print("You are below 18")
This is the Output这是 Output
Enter Your Age: 20
Your are 18+
You are below 18
Age > 18
it should print "Your are 18+"
Age > 18
时,它应该打印"Your are 18+"
Age == 18
it should print "Your are 18"
Age == 18
时,它应该打印"Your are 18"
Age < 18
it should print "Your are below 18"
What is happening here?Age < 18
时,它应该打印"Your are below 18"
这里发生了什么?Use if elif else
ladder.使用
if elif else
阶梯。 Don't use pass
.不要使用
pass
。 pass
is when you do nothing inside a branching condition. pass
是当你在分支条件内什么都不做的时候。
In your code,在您的代码中,
if
gets evaluated.if
被评估。 As it's true, the print statement gets executed.if
will get executed.if
将被执行。 As it's false
, the condition is false, it won't go inside the if
block.false
,所以条件是 false ,它不会在if
块内 go 。else
block.else
块。 Because the else
is tied with the second if
the context for this is only second if
if
this 的上下文仅是第二个if
,则else
与第二个相关联 Age = int(input("Enter Your Age: "))
if (Age > 18):
print("Your are 18+")
elif (Age == 18):
print("Your are 18")
else:
print("You are below 18")
If you want to do with if/else
only如果您只想使用
if/else
Age = int(input("Enter Your Age: "))
if (Age > 18):
print("Your are 18+")
else:
if (Age == 18):
print("Your are 18")
else:
if (Age < 18):
print("You are below 18")
change your second if statement to elif.将第二个 if 语句更改为 elif。 It doesn't work currently because else is comparing to your second if condition.
它目前不起作用,因为 else 正在与您的第二个 if 条件进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.