Python 需要理解为什么我的函数以这种方式工作

Python 需要理解为什么我的函数以这种方式工作,python,function,input,Python,Function,Input,我刚开始学习python,在一些关于我为了练习而做的函数的练习中,我想到了以下简单的想法,我想稍后以更大的方式进行测试,下面是我的代码: def test_function(firstn): firstn = input("introduce your name: ") ## assigns the value of the introduced value print(firstn + " Perez") ##prints the function + the surname

我刚开始学习python,在一些关于我为了练习而做的函数的练习中,我想到了以下简单的想法,我想稍后以更大的方式进行测试,下面是我的代码:

def test_function(firstn):
    firstn = input("introduce your name: ") ## assigns the value of the introduced value
    print(firstn + " Perez") ##prints the function + the surname

 test_function(0) ## calls the function and executes
一切正常,问题是,当我第一次在调用函数“test_function”的最后一行完成代码时,如果我未指定括号之间的值,代码失败,并显示以下错误消息:

def test_function(firstn):
    firstn = input("introduce your name: ")
    print(firstn + " Perez")

test_function() ## without any value

"TypeError: test_function() missing 1 required positional argument: 'firstn'" 
我的问题是,当我调用括号中有任意随机数的函数时,它为什么起作用,而当我只调用括号中没有任何值的函数时,它却不起作用


感谢您提供的任何反馈或任何建议,以改进我对本手册的使用

您已将函数定义为只接受一个参数的函数:
def test_函数(firstn):
。这就是为什么如果您尝试在没有参数的情况下调用它,它会抛出异常。请注意,您没有使用此参数,只是用函数中的第一行将其重写:
firstn=input(“介绍您的名字:”)

因此,您的函数可能应该:

  • 不接受任何参数
    def test_function():

  • 以某种方式使用那个论点

您已将函数定义为只接受一个参数的函数:
def test_函数(firstn):
。这就是为什么如果您尝试在没有参数的情况下调用它,它会抛出异常。请注意,您没有使用此参数,只是用函数中的第一行将其重写:
firstn=input(“介绍您的名字:”)

因此,您的函数可能应该:

  • 不接受任何参数
    def test_function():

  • 以某种方式使用那个论点

发生这种情况的原因是,它对
firstn
没有值,因此如果您有如下函数,则无法更改
firstn

def test(context):
    context = 5
    print(context)

您将需要
context
变量,因为您是第一次在
def test
中定义它,如果您没有context变量,它将看起来像计算机一样,context是函数中随机创建的变量,而且不能在函数中定义变量。

之所以会发生这种情况,是因为它对
firstn
没有值,因此如果您有如下函数,则无法更改
firstn

def test(context):
    context = 5
    print(context)

您将需要
context
变量,因为您是第一次在
def test
中定义它,如果您没有context变量,它将看起来像计算机一样,context是函数中随机创建的变量,您不能在函数中定义变量。

您在函数定义中指定了参数
firstn
。它必须在函数调用中提供,否则会出现异常。
由于您在第一行中立即覆盖了
firstn
变量,因此将其作为参数绝对没有用。

您在函数定义中指定了参数
firstn
。它必须在函数调用中提供,否则会出现异常。
由于在第一行中立即覆盖
firstn
变量,因此将其作为参数绝对没有用。

如果要编写一个可以接受0或1个参数的函数,可以为如下参数提供“默认值”:

def test_function(firstn="Mr"):
    firstn = input("introduce your name: ") ## assigns the value of the introduced value
    print(firstn + " Perez") ##prints the function + the surname

 test_function("Bob") ## calls the function and executes
 test_function() ## calls the function and executes

注意我声明的
def test_function(firstn=“Mr”)
的方式,其中
firstn
有一个“默认值”,即
“Mr”

如果您想编写一个可以接受0或1个参数的函数,您可以为如下参数提供一个“默认值”:

def test_function(firstn="Mr"):
    firstn = input("introduce your name: ") ## assigns the value of the introduced value
    print(firstn + " Perez") ##prints the function + the surname

 test_function("Bob") ## calls the function and executes
 test_function() ## calls the function and executes

请注意我声明
def test_函数(firstn=“Mr”)
的方式,其中
firstn
有一个“默认值”,即
“Mr”

Python函数采用两种类型的参数:必需的位置参数和可选的命名参数。例如,在

def test_function(firstn, middlen=""):
    print(firstn, middlen, "Perez")
调用方必须提供
firstn
,但
middle
是可选的

在您的情况下,您重新分配了
firstn
,因此一开始就不需要它。简单地

def test_function():
    firstn = input("introduce your name: ") ## assigns the value of the introduced value
    print(firstn + " Perez") ##prints the function + the surname

firstn
的赋值将创建一个局部变量“firstn”,并将从
input
返回的值赋给它。此变量在函数退出时被销毁。

Python函数采用两种类型的参数:必需的位置参数和可选的命名参数。例如,在

def test_function(firstn, middlen=""):
    print(firstn, middlen, "Perez")
调用方必须提供
firstn
,但
middle
是可选的

在您的情况下,您重新分配了
firstn
,因此一开始就不需要它。简单地

def test_function():
    firstn = input("introduce your name: ") ## assigns the value of the introduced value
    print(firstn + " Perez") ##prints the function + the surname

firstn
的赋值将创建一个局部变量“firstn”,并将从
input
返回的值赋给它。此变量在函数退出时被销毁。

def test_function(firstn)
定义了一个包含1个必需参数的函数。实际上,您没有使用该名称,而是将其重新绑定到另一个值。您可能只是想
def test_function()
定义一个没有输入参数的函数。请参阅Python教程中的。
def test_function(firstn)
定义一个使用1个必需参数的函数。实际上,您没有使用该名称,而是将其重新绑定到另一个值。可能您只是想
def test_function()
定义一个没有输入参数的函数。请参阅Python教程中的。您可以在函数中创建局部变量。只需指定一个值。在函数中,
foo=“bar”
将导致python创建局部变量“foo”。您可以在函数中创建局部变量。只需指定一个值。在函数中,
foo=“bar”
将导致python创建一个局部变量“foo”。因此,如果我只定义我的函数,我假设它是可以的