+ 2
Python 3 Help Me Please
num = 7 if num > 3: print("3") if num < 5: print("5") if num == 7: print("7") Why Is This Output Is Only 3? 7==7 So,Im little bit confused
24 Answers
+ 5
I made mistakes , I'm sorry. To clearly learn how if elif and else works you should recap that topic in Python. I wish you understand this perfectly.
Happy coding! 👋🙂
+ 11
I would suggest your indentation was causing the problem.
num = 7
if num > 3:
print("3")
if num < 5:
print("5")
if num == 7:
print("7")
This will return both 3 & 7 as both of these queries return True
+ 5
Because num < 5 is False and next if statement never come
+ 4
Output is three becoz first condition is satisfied successfully so it inhibits all other conditions
+ 3
Mirielle(20k XP Monthly till November [2020]) yeah this is right answer too.
+ 3
Rik Wittkopp and your answer is right too.
+ 2
If statement can be usdd only once you should use the elif statement
+ 2
It is beceause python executes a codes by moving line to line. So when it reaches the first condition which verifies.... Then it excecutes.
Plus also the indentation is causing the problem to the execution of the code.
If it is removed it will output both 3&7
+ 2
First of all change all if statements to "elif" except the first one and plz correct the indentation of the if statements..
num = 7
if num > 3:
print("3")
elif num < 5:
print("5")
elif num == 7:
print("7")
This Output Is 7.
+ 2
U need to understand how indentation works in python. It's similar to curly brackets in c and java
Edit : please ignore the best answer, it was marked best by the guy who asked the question who is clearly learning the ropes.
if statement can be used as many times as required. it is not necessary to use elif or else after an if statement
+ 1
You could use if and subsequently elif
Or
Use if without having to indent in that way
Each if should be separate not bringing it iside the others
+ 1
Because num>3 already satisfies the condition and the code stops running from there.
+ 1
Because first statement num>3.
Was TRUE so every other statement is ingnored.
Create multiple SUITS then it will work.
+ 1
In If condition first condition 7>3
So it enters in if statement. Now the main trick comes- second if condition comes in first if statement and third if condition comes under second if statement. Because second if condition 7<5 is wrong so it doesn't enters in second if statement, this way the third if condition is never applied to be checked. Replace all 7 with 4 will see desired effect
+ 1
As the condition is in 3rd if which is nested inside an if statement (num < 5) which will result false so the code written inside it (included third if condition) will not be read by compiler so it will not get executed.
0
thx so much for helping
0
because first statment True print first True
0
Because you assign the value ...Num=7
First if condition is true so print 3
Num>3
Second condition is false so cannot print anything...
"The final answer print 3...."
0
3...coz num<5 is false hence code stops
0
You should have consider the indentation in python code
In this program the all other if statements come under the first if statement
You should write the code like
num=7
If num>3:
print("3")
if num<5:
print("5")
if num==7:
print("7")