Python:返回可能不返回值

时间:2018-11-17 02:46:45

标签: python return syntax-error

Hello Stack溢出社区,

我正在尝试编写一个检查Fermat的最后定理的程序。我遇到的问题是出现一个错误,提示“ NameError:未定义名称'a'。但是,我在第一个函数中定义了'a',然后在函数末尾返回其值。

我试图在第二个功能中使用第一个功能的输入值,以便用户定义参数。

我误会了如何利用“收益”吗?所有帮助将不胜感激,这将使我保持理智。

def input_fermat():
    a=input('Enter the first variable \'a\': \n')
    b=input('Enter the second variable \'b\': \n')
    c=input('Enter the third variable \'c\': \n')
    n=input('Enter the exponential variable \'n\': \n')

    return a, b, c, n

def check_fermat(a,b,c,n):

    calc_1=a**n
    calc_2=b**n
    calc_3=c**n

    if n>2 and int(calc_1) + int(calc_2) == calc_3:
        print('Holy smokes, Fermat was wrong!')
    else:
        print('No that doesn\'t work.')

input_fermat()
check_fermat(a,b,c,n)

5 个答案:

答案 0 :(得分:2)

您没有存储函数input_fermat返回的值。试试:

a, b, c, n = input_fermat()

check_fermat(a,b,c,n)

答案 1 :(得分:1)

a, b, c, n中定义的变量input_fermat仅存在于函数中,这就是为什么要返回它们的原因,但是在调用函数时,并没有将它们保存在任何地方。您应该替换:

input_fermat()

通过:

a, b, c, n = input_fermat()

或者您可以像这样直接将input_fermat的返回值传递给check_fermat

check_fermat(*input_fermat())

答案 2 :(得分:1)

之所以会发生这种情况,是因为这些变量是在本地定义的,并且在check_fermat的命名空间中不可用。

请参阅LEGB规则。

您可以做的是使用函数定义中的global关键字定义所有这些变量,尽管通常这不是最佳方法。您还需要将所有输入转换为整数,因为input()将返回一个字符串。

答案 3 :(得分:0)

返回的值不仅会自动显示在名称空间中,还必须将它们分配给某些内容。

a, b, c, n = input_fermat()

答案 4 :(得分:0)

input_fermat()中作为输入接收的变量a,b,c和n仅在该函数的主体内可用;返回后,您就超出了input_fermat()的范围,a,b,c和n中的 values 会传递给您调用input_fermat()的任何变量进行分配

一个函数的作用域意味着在任何给定函数中唯一可用的变量是

  • 在函数主体中声明的那些
  • 那些作为括号传递给函数的函数。
  • 全局声明的变量

check_fermat()中,这表示您可以根据需要重新使用变量a,b,c和其他输入(因为新功能意味着新作用域)。

但是,在下面显示的代码中,我们决定check_fermat()中的a,b,c和n与input_fermat()中的a,b,c和d相同,并且声明a,b,c,n = input_fermat()。这是我们选择做出的决定;这是任意的。

这是功能的编辑版本,可以实现我认为的目标:

#Global variables would be declared up here, before all other function declarations.
#Global variables would be available to all the functions that follow.

def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
    # input_fermat() scope begins
    a=int(input('Enter the first variable \'a\': \n'))
    b=int(input('Enter the second variable \'b\': \n'))
    c=int(input('Enter the third variable \'c\': \n'))
    n=int(input('Enter the exponential variable \'n\': \n'))

    return a, b, c, n
    #input_fermat() scope ends

def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
    #check_fermat() scope begins

    #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
    a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
    calc_1=a**n
    calc_2=b**n
    calc_3=c**n

    if n>2 and int(calc_1) + int(calc_2) == calc_3:
        print('Holy smokes, Fermat was wrong!')
    else:
        print('No that doesn\'t')

    #python implicitly does a `return None` here
    #check_fermat() scope ends

check_fermat()

请注意,由于这些功能的范围,我本可以在check_fermat()中声明变量,如下所示,它们仍然可以正常工作(尝试运行此代码供自己查看)

def input_fermat(): 
    a=int(input('Enter the first variable \'a\': \n'))
    b=int(input('Enter the second variable \'b\': \n'))
    c=int(input('Enter the third variable \'c\': \n'))
    n=int(input('Enter the exponential variable \'n\': \n'))

    return a, b, c, n

def check_fermat():
    any,variable,names,go = input_fermat()
    calc_1=any**go
    calc_2=variable**go
    calc_3=name**go

    if n>2 and int(calc_1) + int(calc_2) == calc_3:
        print('Holy smokes, Fermat was wrong!')
    else:
        print('No that doesn\'t')

check_fermat()

(两个代码段的)执行过程如下:

    最后一行上的
  1. check_fermat()被执行,因为它是我们的.py文件中唯一调用的函数(不仅仅是定义的函数)。
  2. Python寻找check_fermat()的定义来执行它 3.Python发现input_fermat()check_fermat内部被调用,并继续寻找input_fermat()的定义。
  3. Python找到定义,执行函数并要求输入。
  4. 输入返回到check_fermat(),并用于计算费马的最后定理。
  5. 执行check_fermat()的其余部分(输出打印到终端)。然后,check_fermat()返回None,不返回任何变量结束函数调用。
相关问题