Please, I need someone to explain to me the indentation of the else statement. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Please, I need someone to explain to me the indentation of the else statement.

https://code.sololearn.com/c343etna9zTn/?ref=app

31st Jul 2022, 3:08 PM
Rebecca
Rebecca - avatar
9 ответов
+ 5
Rebecca , a general recommendation about indentation of python code: >>> a good and correct indentation gives a good readability of the code. it is recommended to use 4 spaces per indentation level. this is the official style guide for python (PEP 8): https://peps.python.org/pep-0008/#indentation
31st Jul 2022, 7:13 PM
Lothar
Lothar - avatar
+ 3
Instead of clicking 2 or 3 times for space I would suggest use tab which takes 4 space. If you use tab, indentation will be always correct
31st Jul 2022, 7:39 PM
A͢J
A͢J - avatar
+ 2
Lothar I normally use three spaces for indentation in Python. Four spaces are too much after my opinion, and in the beginning, I even only used two spaces, but later I found out that three spaces was great for me.
31st Jul 2022, 7:27 PM
Jan
Jan - avatar
+ 2
Rebecca if an else statements also have the same indentation, but a for loop can also have an else statement.
31st Jul 2022, 7:39 PM
Jan
Jan - avatar
+ 2
A͢J I'm old fashioned. I cannot have all those spaces😂😂
31st Jul 2022, 7:49 PM
Jan
Jan - avatar
+ 1
It's more convenient doing it this way instead. contacts = dict(contacts) a = input() if a in contacts: print(a + " is " + str(contacts[a])) else: print("Not found")
31st Jul 2022, 3:31 PM
Jan
Jan - avatar
+ 1
For loop statement and the else statement have the same indentation but i was expecting the if and else statements to have the same indentation.
31st Jul 2022, 7:31 PM
Rebecca
Rebecca - avatar
+ 1
I think going by the default in most Python IDLE is good way to have it on codes.
1st Aug 2022, 3:10 PM
Sobola Gabriel
Sobola Gabriel - avatar