+ 7

is there any mistke in this code

x=5 if x==4: print ("yesss") else : print ("noo")

23rd Dec 2020, 10:54 AM
Lisa Sanal
Lisa Sanal - avatar
39 Respuestas
+ 22
You have to care about indentation when you're using if-else statement x=5 if x==4: print ("yesss") else : print ("noo") Edit: Check it out for reference. https://code.sololearn.com/cT5BRIbkia21/?ref=app You can compare my code with your code to understand it clearly
23rd Dec 2020, 10:58 AM
Simba
Simba - avatar
+ 14
tnx
7th Feb 2021, 7:07 AM
Melekte Petros
Melekte Petros - avatar
+ 9
There is no indentation python interpretor will give you an error
23rd Dec 2020, 10:56 AM
Krish
Krish - avatar
+ 5
A space " " x = 2 if x == 2: print(x) else: print(x +1) Here in this case the output must be 2 but the output will be 2 3 Because there is not gap in the else part like I have in the if part. That means that will execute even if the condition is true because it is not the part of the else statement. You need a space before starting a new line for function, loop, conditions in python for the.
23rd Dec 2020, 11:09 AM
Krish
Krish - avatar
+ 5
x = 5 if x==4: print("yesss") else: print("noo")
25th Dec 2020, 12:17 AM
Melekte Petros
Melekte Petros - avatar
+ 4
Usman Muhammed this is the wrong place for advertising. use any advertising thread for this
23rd Dec 2020, 1:45 PM
Krish
Krish - avatar
+ 3
Indentation mistake
24th Dec 2020, 9:34 AM
‎Keshav
‎Keshav - avatar
+ 3
Actually the code runs , it does exactly what i want. The indentation is fine if u taka a closer look... Am just trying to use a function to get an output as that.. Like def number_type(number): 👆 Something like that
24th Dec 2020, 12:20 PM
Ogboi Favour
Ogboi Favour - avatar
+ 2
Lisa Sanal Yes, the courses are free
23rd Dec 2020, 11:18 AM
SpaceBarMönkey
SpaceBarMönkey - avatar
+ 2
kk
23rd Dec 2020, 11:20 AM
Lisa Sanal
Lisa Sanal - avatar
+ 2
Your code doesn't have indentation for the if statements, therefore you will get an error.
23rd Dec 2020, 5:01 PM
Jesse Sambo
Jesse Sambo - avatar
+ 2
This tells the number_type a user inputs Tells if its a prime number, odd number or even number. But i am finding it hard to use functions to get the same output. Number = int(input("enter number: ")) if Number % 2 == 0: print("Its an even number") elif Number % 2 != 0: print("Its an odd number") elif Number % 2 != 0 and Number % Number == 0 and Number % 3 != 0: print("its a prime number")
23rd Dec 2020, 10:59 PM
Ogboi Favour
Ogboi Favour - avatar
+ 2
When you have : in one program , then next lines must have tab . This time you have if . If have : and you must write If x==4: print("yessss") else : print("noo")
24th Dec 2020, 12:01 PM
Amaklia Ilkama
Amaklia Ilkama - avatar
+ 2
I think that's right
25th Dec 2020, 9:29 AM
Riselda Rada
Riselda Rada - avatar
+ 2
Lisa Sanal Since your question has been answered sufficiently it would be nice if you could mark it as such to avoid more redundant posts. ;)
25th Dec 2020, 4:20 PM
SpaceBarMönkey
SpaceBarMönkey - avatar
+ 1
Yes you are correct. There uses curly brackets {} for the statement inside the loop
23rd Dec 2020, 11:12 AM
Krish
Krish - avatar
+ 1
If you write this code on an IDE, it will automatically add indentation to it. But you wrote it here without adding indentation. Nothing else is wrong
24th Dec 2020, 4:13 PM
Mohed
Mohed - avatar
+ 1
Lisa Sanal There is a minor mistake. You must use indentation properly to make this code work. It should be like this-- x =5 If x==4: print ("yesss") else: print("noo") Just an additional piece of advice, when you make such syntactical mistakes, Python gives you an error msg. Please read them carefully and you will learn to correct them on your own.
24th Dec 2020, 4:20 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 1
He gives noo Why x not is equal of 4
24th Dec 2020, 10:05 PM
Andrea Barreto
Andrea Barreto - avatar
+ 1
indentation is very important and keep that in mind using tabs and spaces together will cause problem
24th Dec 2020, 11:09 PM
Siamak Saeedi