![]() |
|
"return" value indentation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: "return" value indentation (/thread-27730.html) |
"return" value indentation - extricate - Jun-19-2020 Hi, Regarding the code below phone_letters = ["' '","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
def let_to_num():
letter = input("Enter letter: ")
key = 0
while key < 10:
if letter.upper() in phone_letters[key]:
return key
else:
key = key + 1
return "Not found"
print(let_to_num())
What is the significance of placing the return "Not found" at the same indentation as "while key < 10:"?as compared to phone_letters = ["' '","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
def let_to_num():
letter = input("Enter letter: ")
key = 0
while key < 10:
if letter.upper() in phone_letters[key]:
return key
else:
key = key + 1
return "Not found"
print(let_to_num())
By doing it as per the second code, the whole code will crumble and wont work? I can't get my head around this
RE: "return" value indentation - buran - Jun-19-2020 Isn't it clear - the difference is what will happen if letter is not in the first element of phone_letter.You can use http://www.pythontutor.com/visualize.html to visualize the execution of the code and get better understanding of what's going on. As a side note - it's easier to use dict, not list RE: "return" value indentation - extricate - Jun-19-2020 great aid. thanks RE: "return" value indentation - oliv2 - Jun-19-2020 Hi, In the first code, the return doesn't depend of the "if then else" does it ? Python indentation marks blocks as begin/end in others programming languages. |