0

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()
1
  • 2
    m is not defined in main() Commented Mar 2, 2018 at 1:38

1 Answer 1

5

When you call your message() function, you need to store the return values.

def main():
    m, t = message()
    cipher(m, t)
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Thank You. Whenever I want to return a value from a function in python will I need to do this ?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.