python function called but not returning value. Sh

2020-05-01 01:13发布

问题:

I've got a function:

def user_login(m):
    m = "user_login function called"
    return m

Calling it with:

user_login(message)

It should change a string, m, and return the result. I know that the function gets called because it throws up an error [user_login() takes exactly 1 argument (0 given)] if I don't put an argument in it. But it doesn't return a string. How can I find out what's wrong?

回答1:

You can't "change the string" - instead, what you should be doing, is assigning the result of the function to your string in the calling scope:

m = user_login('some message')
print m