+ 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

31st Mar 2020, 8:58 AM
Kerem
24 Antworten
+ 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! 👋🙂
31st Mar 2020, 9:14 AM
Aquizz
Aquizz - avatar
+ 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
31st Mar 2020, 9:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
Because num < 5 is False and next if statement never come
1st Apr 2020, 1:34 PM
Александр Васильев
Александр Васильев - avatar
+ 4
Output is three becoz first condition is satisfied successfully so it inhibits all other conditions
2nd Apr 2020, 7:51 AM
Wade
Wade - avatar
+ 3
Mirielle(20k XP Monthly till November [2020]) yeah this is right answer too.
31st Mar 2020, 9:21 AM
Aquizz
Aquizz - avatar
+ 3
Rik Wittkopp and your answer is right too.
31st Mar 2020, 9:23 AM
Aquizz
Aquizz - avatar
+ 2
If statement can be usdd only once you should use the elif statement
1st Apr 2020, 6:26 PM
Shashank
Shashank - avatar
+ 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
2nd Apr 2020, 12:21 AM
Josaphat NGOGA UWIZEYE
Josaphat NGOGA UWIZEYE - avatar
+ 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.
2nd Apr 2020, 12:34 AM
Amitesh Singh
Amitesh Singh - avatar
+ 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
2nd Apr 2020, 7:42 AM
Bhavya
Bhavya - avatar
+ 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
1st Apr 2020, 8:15 AM
Nfon Andrew Tatah
Nfon Andrew Tatah - avatar
+ 1
Because num>3 already satisfies the condition and the code stops running from there.
1st Apr 2020, 5:46 PM
João Cláudio Macosso
João Cláudio Macosso - avatar
+ 1
Because first statement num>3. Was TRUE so every other statement is ingnored. Create multiple SUITS then it will work.
1st Apr 2020, 6:58 PM
Tarun
Tarun - avatar
+ 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
1st Apr 2020, 7:16 PM
Uttam Pandey
+ 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.
1st Apr 2020, 8:53 PM
Samar Fatima
Samar Fatima - avatar
0
thx so much for helping
31st Mar 2020, 9:16 AM
Kerem
0
because first statment True print first True
31st Mar 2020, 8:20 PM
Samar Alaa Eldin
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...."
1st Apr 2020, 11:24 AM
VS Vinoth
VS Vinoth - avatar
0
3...coz num<5 is false hence code stops
1st Apr 2020, 11:36 AM
WaseEm BhatTi
WaseEm BhatTi - avatar
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")
2nd Apr 2020, 4:09 AM
Vinay Kushwaha
Vinay Kushwaha - avatar