Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Return可能不返回值_Python_Return_Syntax Error - Fatal编程技术网

Python:Return可能不返回值

Python:Return可能不返回值,python,return,syntax-error,Python,Return,Syntax Error,你好,Stack Overflow社区 我正试图写一个程序来检查费马的最后一个定理。我遇到的问题是出现了一个错误,显示“NameError:name'a'未定义。但是,我在第一个函数中定义了'a',并在函数末尾返回其值 我试图在第二个函数中使用第一个函数输入的值,以便用户可以定义参数 我是否误解了如何利用“回报”?所有的帮助都会让我保持理智 def input_fermat(): a=input('Enter the first variable \'a\': \n') b=in

你好,Stack Overflow社区

我正试图写一个程序来检查费马的最后一个定理。我遇到的问题是出现了一个错误,显示“NameError:name'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)

您没有存储函数
input\u fermat
返回的值。请尝试:

a, b, c, n = input_fermat()

check_fermat(a,b,c,n)

您没有存储函数
input\u fermat
返回的值。请尝试:

a, b, c, n = input_fermat()

check_fermat(a,b,c,n)

input\u fermat
中定义的变量
a、b、c、n
仅存在于函数中,这就是您返回它们的原因,但调用函数时,您不会将它们保存在任何地方。您应该替换:

input_fermat()
作者:

或者您可以直接将
input\u fermat
的返回值传递给
check\u fermat
,如下所示:

check_fermat(*input_fermat())

input\u fermat
中定义的变量
a、b、c、n
仅存在于函数中,这就是您返回它们的原因,但调用函数时,您不会将它们保存在任何地方。您应该替换:

input_fermat()
作者:

或者您可以直接将
input\u fermat
的返回值传递给
check\u fermat
,如下所示:

check_fermat(*input_fermat())

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

a, b, c, n = input_fermat()

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

a, b, c, n = input_fermat()

这是因为这些变量是在本地定义的,在check_fermat的命名空间中不可用

请参阅-规则


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

发生这种情况是因为这些变量是在本地定义的,在check\u fermat的命名空间中不可用

请参阅-规则


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

变量a、b、c和n,这些变量在
input\u fermat()中作为输入接收
,仅在该函数体中可用;一旦返回,就超出了
input\u fermat()
的作用域,a、b、c和n中的值将被传递给调用
input\u fermat()
的任何变量进行赋值

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

  • 在函数体中声明的
  • 作为括号中的参数传递给函数的
  • 全局声明的变量
check_fermat()
中,这意味着您可以根据需要将变量a、b、c和重新用于输入以外的内容(因为新函数意味着新范围)

但是,在下面显示的代码中,我们决定
check\u fermat()
中的a、b、c和n将与
input\u fermat()
中的a、b、c和d相同,声明为
a、b、c、n=input\u 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\u fermat()
中声明变量,如下所示,所有这些变量仍然可以工作(请尝试运行此代码以供自己查看)

执行过程(对于两个代码段)如下所示:

check_fermat(*input_fermat())
  • 执行最后一行的
    check\u fermat()
    ,因为它是.py文件中唯一调用(而不仅仅是定义)的函数
  • Python查找
    check\u fermat()
    的定义来执行它 3.Python查找
    input\u fermat()
    check\u fermat
    内部调用,并查找
    input\u fermat()
    s定义
  • Python找到定义,执行函数并请求输入
  • 输入返回到
    check\u fermat()
    ,用于计算fermat的最后一个定理
  • 执行剩余的
    check\u fermat()
    (输出打印到终端)。然后,
    check\u fermat()
    返回无,结束函数调用,不返回任何变量

  • input\u fermat()
    中作为输入接收的变量a、b、c和n仅在该函数体中可用;一旦返回,就超出了
    input\u fermat()
    的范围,a、b、c和n中的值将被传递给调用
    input\u fermat()
    的任何变量进行分配

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

    • 在函数体中声明的
    • 作为括号中的参数传递给函数的
    • 全局声明的变量
    check_fermat()
    中,这意味着您可以根据需要将变量a、b、c和重新用于输入以外的内容(因为新函数意味着新范围)

    但是,在下面显示的代码中,我们决定
    check\u fermat()
    中的a、b、c和n将与
    input\u fermat()
    中的a、b、c和d相同,声明为
    a、b、c、n=input\u 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\u fermat()
    中声明变量,如下所示