gpt4 book ai didi

python - 如何修复名称错误: name 'author' is not defined when running python script

转载 作者:行者123 更新时间:2023-11-30 22:01:08 24 4
gpt4 key购买 nike

我试图在我的 Mac 终端上打开它,但我总是收到名称“作者”未定义的信息,而它显然是定义的。

def bibformat_mla(author, title, city, publisher, year):
author = input("enter author: ")
title = input("enter title: ")
city = input("enter city: ")
publisher = input("enter publisher: ")
year = input("enter year: ")
answer = author + ' , ' + title + ' , ' + city + ': ' + publisher + ', ' + year
return answer


bibformat_mla(author, title, city, publisher, year)
'author, title, city: publisher, year'

bibformat_mla("Jake, Matt", "Open Data ", "Winnipeg", "AU Press", 2013)
'Morin, Pat. Open Data Structures. Winnipeg: AU Press, 2013'

最佳答案

当您运行以下命令时:

bibformat_mla(author,title,city,publisher,year)

您对程序说您有一个名为“author”的变量,它已准备好传递给 biblformat()。这会导致错误,因为在调用函数之前 undefined variable 。

即,您告诉函数需要某个变量,它会向您抛出错误,因为该变量实际上还不存在。

从您想要实现的目标来看,您可以简单地调用该函数,如下所示:

bibformat_mla()

您还需要将定义更改为此,以便您的函数不再需要参数:

def bibformat_mla():

关于python - 如何修复名称错误: name 'author' is not defined when running python script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com