+ 1
What is indentation block? (Python)
if 1+1==2 if 2*2==8 print: ('Eight') Else print ("2") I try to run this code but it gives error. Can any human help?
6 Réponses
+ 8
That means that after each structure you must have an indentation accordingly. For example:
if 1 + 1 == 2
****if 2 * 2 == 8:
****print: ('Eight')
else:
****print ("2")
The asterisks are the amount of spaces that must be after defining a structure. That is, if something belongs to a code block, it must be tabulated accordingly.
+ 7
I'm assuming you're using Python. In any case, you have some errors:
Python expects you to correctly indent your code, and in your case there are things that are not correctly defined (for example, I am not able to define if your else belongs to the first or second if). Also keep in mind that all structures start in lowercase. In the same way you forgot to put the ':'
Your code should look like this (I'm not sure about the else statement):
if 1+1 == 2:
if 2*2 == 8:
print('Eight')
else:
print ("2")
+ 2
Mickel is right on... An indentation block is a block of code that is indented. Like when you use the if/else, the content inside each of them is indented. In Python this is required, in some other languages it isn't required but still may be practiced just for better structured code. Many people use a single tab for the indent, however it is usually recommended that you use 4 spaces instead. This is because some interpreters handle tabs a little differently, spaces will still be the same though. Here's a little example...
x = input('yes or no?')
msg1 = ['You chose yes!']
msg2 = ['You chose no!']
message = []
if x == 'yes':
message.append(msg1[0])
print(message[0])
elif x == 'no':
message.append(msg2[0])
print(message[0])
else:
print('Invalid input!')
print('Sorry...')
print('Rerun to try again.')
So, the indentation blocks would be each block of code that is inside of the if/elif/else statements. This code is "indented" 4 spaces.
PS: I know this example has more code than necessary, I only used multiple lists to show multiple indented lines.
+ 1
which language is this? Python?
0
yes
0
what about the indentation block?
what's that?