+ 1
I'm confused about the if-else statement in one of the lessons
The lesson went like "age = 30 if age <= 18: print() else: print() print()" It prints out any leftover outputs supposedly instead of just printing out and if or else output, but I don't know how to wrap my head around it. Can someone help break it down for me?
1 Answer
+ 7
If...else statement works like this.
If the condition is True, then perform This; otherwise, do That.
With your code provided (and I added little detail, like a reception in front of a bar)
age = 30
if age <= 18:
print("Not Allowed")
else:
print("Welcome")
Variable "age" holds integer 30, which is greater than 18.
"if age <= 18" becomes "if 30 <= 18", and "30<=18" evaluated to False. Therefore, it skips everything until reaching the "else", thus prints "Welcome".