Python 函数不返回字符串值
我想从我的message函数返回m&t的字符串值,以便在cipher函数中使用,以执行while循环,并在错误时反向打印消息。我收到的错误消息是“NameError:name'm'未定义”,但“m”已在消息中定义,我正试图返回该消息以便与“t”一起在密码中使用Python 函数不返回字符串值,python,function,return-value,Python,Function,Return Value,我想从我的message函数返回m&t的字符串值,以便在cipher函数中使用,以执行while循环,并在错误时反向打印消息。我收到的错误消息是“NameError:name'm'未定义”,但“m”已在消息中定义,我正试图返回该消息以便与“t”一起在密码中使用 def main(): message() cipher(m, t) def message(): m = input("Enter your message: ") t = '' return
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()
调用
message()
函数时,需要存储返回值
def main():
m, t = message()
cipher(m, t)
调用
message()
函数时,需要存储返回值
def main():
m, t = message()
cipher(m, t)
m未在工作的
main()
m未在main()
中定义!非常感谢。每当我想从python中的函数返回值时,我需要这样做吗?您也可以执行cipher(message())
。函数只返回值。函数中声明的变量不会离开调用它们的函数的作用域。范围在我所知道的所有编程语言中都是一个非常重要的概念。成功了!非常感谢。每当我想从python中的函数返回值时,我需要这样做吗?您也可以执行cipher(message())
。函数只返回值。函数中声明的变量不会离开调用它们的函数的作用域。范围在我所知道的所有编程语言中都是一个非常重要的概念。