what’s wrong in this code?
@mohdamaan2805 In python identation is everything because there are no braces or brackets for code. So you should write the following code in this manner:
Result of this is:
Identation menas the code of if block should be one level deeper than the normal code, the code will clear your doubts
Do close the query by clicking on the solution button
You missed indentation in the code every line that’s part of the if or else statement should begin after some space so it all can be considered as part of one block and the subsequent lines after that can be written without spaces…
e.g
maths =“yes”
if maths == “yes”:
___print(“you will get toffee”)
else:
___print(“you will not get anything”)
print(“outside the conditional statement”)
The underscore is space since bolt forum is not showing spaces I have used underscore.
If you are using some conditions by using like loop,etc in Python , then INTENDETION is very important …you have to use INTENDENTION over evry if,elif statement
The problem is with inappropriate indentation, or lack of indentation to be precise.
All the codes that need to be executed if the condition is true must be indented rightwards. In your codes, you have not provided any statement to be executed if the maths == yes holds true.
It was an indentation error. The indentation error can occur when the spaces or tabs are not placed properly and u have to include brackets too.
So u have to write the above code as like
maths = “yes”
if(maths == “yes”):
print(“You will get toffee”)
else:
print(“You will not get anything”)
print(“Outside the conditional statement”)
In python indentation is everything because there are no braces or brackets for code. So you should write the following code in well manner.
indentation is everything because there are no braces or brackets for code. So you should write the following code in well manner.
eg:-
a = “Yes”
if(a==“Yes”):
print(“you will get toffee”)
else:
print(“you will not get anything”)
This was an indentation error. In python whenever you code proper indentation is must, otherwise it would always give an error.
simply u have to do is after any logical statement give a space like tab key
hello @mohdamaan2805 . actually it is indentation error. you have to use brackets while using if condition. like,
if(maths == “yes”):
print(“You will get toffee”)
NOW TRY TO SOLVE AGAIN AND SEE WHAT HAPPENS
You missed indentation in the code. If you’re condition statement then the print which you want to do should come under your condition statement.
e.g
maths =“yes”
if maths == “yes”:
print(“you will get toffee”)
else:
print(“you will not get anything”)
print(“outside the conditional statement”)
maths = “yes”
if(maths == “yes”):
print(“You will get toffee”)
else:
print(“You will not get anything”)
print(“Outside the conditional statement”)
This is the correct code for your explanation
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. So first choose the block of the conditional statement then follow the proper indentation rule to check whether it is correct or not
Hi
I think the code would be like this.
maths = “yes”
if maths == “yes”:
print(“You will get toffee.”)
else:
print(“You will not get anything.”)
print(“Out side of conditional statements”)
python uses indentation to determine the scope of the program.
Scope is how Python knows what code belongs to what part of the program.
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
The corrected code is as follows:
maths=“yes”
if maths == “yes”:
print(“you will get toffee”)
else:
print(“you will not get anything”)
print(“outside the conditional statement”)
Hi @mohdamaan2805, the mistake you have made in this code is “INDENTATION”.
Which is very important while coding in python.
So, If you want to apply a condition you have to write the code by giving indentation for the given condition.
There was an indentation error. The code should be as follows:
maths == “yes”
if (maths == “yes”) :
print ( “You will get toffee.” )
else:
print ( “You will not get anything” )
print( " Outside of conditional statements " )
Python wholly relies on indentation for the block of code, so use indentation and the problem will be solved.