I would like to return the string values of m & t from my message function to use in cipher function to perform a while loop and once false print the message reverse. The error message I am receiving is "NameError: name 'm' is not defined", but 'm' has been defined in message which i am attempting to return for use in cipher along with 't'.
def main():
message()
cipher(m, t)
def message():
m = input("Enter your message: ")
t = ''
return m, t
def cipher(m, t):
i = len(m) - 1
while i >= 0:
t = t + m[i]
i -= 1
print(t)
if __name__ == '__main__': main()
asked Mar 2, 2018 at 1:36
user3574939
8294 gold badges19 silver badges37 bronze badges
1 Answer 1
When you call your message() function, you need to store the return values.
def main():
m, t = message()
cipher(m, t)
answered Mar 2, 2018 at 1:39
kingkupps
3,5542 gold badges20 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user3574939
That worked! Thank You. Whenever I want to return a value from a function in python will I need to do this ?
kingkupps
You could also do
cipher(message()). Functions only return values. Variables declared in functions do not leave the scope of the function they are called in. Scope is a really important concept in all programming languages that I know of. python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html lang-py
main()