I am trying to simulate a very simple text editor that accepts lines of text from the user, combines the text into a single string and then prints the string to a file. When I ran my code I discovered that the file contents were simply 'None'.
I traced the code and found that the resultant string being returned from the core function getpayload() is 'None' although the string is built properly right up until the point of return.
The code (stripped of file I/O executed as is):
def getpayload(s):
temp = getln()
if temp.upper() != "END":
s += temp
getpayload(s)
else:
return s
def getln():
txt = input("|> ")
return str(txt)
def start():
text = getpayload("")
print(text)
if __name__ == "__main__":
start()
When run the following is produced:
$ python3.2 editor.py
|> Line one
|> Line two
|> Line three
|>
|> Line five
|> END
$ None
Printing the string in the else statement produces output of the string as expected. I have also enclosed the return string in str().
I have researched other methods of producing the desired output but I am new to python and I am interested in learning why this sort of issue would occur.