为什么PyCharm给我一个未解决的参考名称错误

问题描述:

我正在学习Python的教程。我写的代码,你看到如下:为什么PyCharm给我一个未解决的参考名称错误

creatures = [] 

def get_creature_race(): 
    creature_race = [] 
    for creature in creatures: 
     creature_race.append(creature["race"].title()) 
    return creature_race 


def print_creature_race(): 
    creature_race = get_creature_race() 
    print(creature_race) 


def add_creature(race, sex): 
    creature = {"race": race, "creature_sex": sex} 
    creatures.append(creature) 


def save_file(creature): 
    try: 
     f = open("creatures.txt", "a") 
     f.write(creature + "\n") 
     f.close() 
    except Exception: 
     print("Could not save file") 


def read_file(): 
    f = open("creatures.txt", "r") 
    for creature in f.readlines(): 
     add_creature(creature) 
    f.close() 


read_file() 
print_creature_race() 


creature_race = input("Enter creature race: ") 
creature_sex = input("Enter creature sex: ") 

add_creature(creature_race, creature_sex) 
print_creature_race() 
save_file(creature_race) 

当我尝试运行此我得到如下:

...line 32, in read_file 
    add_creature(creature) 
TypeError: add_creature() missing 1 required positional argument: 'sex' 

第32行是在READ_FILE()函数定义四号线:

add_creature(creature) 

如果我将鼠标悬停在最右边的括号 ')' 我得到:

Parameter 'sex' unfilled 

如果我把第二个参数:

def read_file(): 
    f = open("creatures.txt", "r") 
    for creature in f.readlines(): 
     add_creature(creature, sex) 
    f.close() 
    print("Could not read file") 

我得到:

line 32, in read_file 
    add_creature(creature, sex) 
NameError: name 'sex' is not defined 

当我将鼠标悬停在性爱这让我:

Unresolved reference sex 

如果我把一个try块def def read_file(),所以我可以运行程序,而不会出错:

def read_file(): 
    try: 
     f = open("creatures.txt", "r") 
     for creature in f.readlines(): 
      add_creature(creature, sex) 
     f.close() 
    except Exception: 
     print("Could not read file") 

并运行它,我得到以下几点:

Could not read file 
[] 
Enter creature race: giant 
Enter creature sex: male 
['Giant'] 

Process finished with exit code 0 

第一行是从READ_FILE()函数的异常,因为creatures.txt尚不存在(或因此我想)。 []是print_creature_race()函数的输出,因为它当前为空。接下来的两个“Enter ...”语句应该是不言自明的。 ['Giant']是第二个print_creature_race()函数调用的输出。我现在有一个creatures.txt文件,其中包含“巨人”一词。

现在,按照教程,如果我跑这第二次是我应该得到的是:

giant 

Enter creature race: elf 
Enter creature sex: female 

有没有异常,因为文件creatures.txt现在存在。 print_creature_race()函数调用导致“巨人”被打印。我得到两个提示进入另一场比赛和性别。不过,我实际上得到的是:

Could not read file 
[] 
Enter creature race: 

如果我继续提示我得到:

Could not read file 
[] 
Enter creature race: elf 
Enter creature sex: female 
['Elf'] 

我creatures.txt文件有巨人,精灵(各自成一行),但是,很明显,我仍然无法读取文件。 add_creature的定义有两个参数,但是包含它并且不包含它会导致错误。

我在做什么错?

+0

要么你有下面的教程有问题,要么你不明白。没有生物的性别,你不能使用'add_creature'。如果文件只包含可能的物种,那么你可能不应该添加到生物列表。而且,如果不告诉我们这是哪个教程,我们不能说你是错的还是教程简单地被破坏了。 – Bakuriu

+0

您可能需要在'creature'旁边的文件中将'creature_sex'写入空格,并在读取文件时使用'line.split()'解析每行。不要忘记在每个函数中添加新参数:save_file ...并且当你打电话给他们时 – PRMoureu

+0

Bakuriu,显然我不明白。这就是我问这个问题的原因。我明白函数调用如何与其他语言一起工作,这就是为什么我不明白为什么教程只用一个参数调用函数。我不明白Python,所以我在问希望有人能向我解释'为什么'。不知道为什么仅仅提出问题就需要反对投票。即使它不是你... –

请了解如何调试您的代码! 我只是提供一些提示。然而,这些并不会导致更迭(保罗留斯指出)

修复最后一行:

save_file(creature_race)

save_file(creatures)

简要说明: 要保存只有比赛,而你的职能在等待:种族和性别。

您还应该查看hot以适当地将您的数据保存到文件中并阅读它。 (例如检查python + csv)。

另外:你创建的函数是坏的 - 它们没有被封装。尝试以某种方式重新思考代码,您的任何函数都不必调用或更改外部变量。然后,您可以启动您的代码,而不执行任何函数,并逐个调用它们以查看它们的功能。观察你的全局变量,随时更改和修复代码。

+0

这不会有帮助,因为生成的文件既不包含生物名称也不包含性别,但是包含名为'creatures'的Python列表的默认格式。本教程似乎不解释如何编写该文件或以允许该脚本运行的方式对其进行解析。也许OP可以告诉我们这个教程来自哪里。 –

+0

确实....没有注意到它。实际上,在理解其他内容的工作方式之前,我不会立即推荐使用这些文件。所有的编程原理,如DRY,封装,以及一个好的算法。 –

+0

save_file(creature_race)是正确的。我仔细检查了教程以确保。当我尝试您的建议时,我也会遇到错误。我正在使用的教程是Python:Pluoplesight上的Bo Milanovich入门。它位于开放,阅读和写作下的第三个模块中...这里是链接:[link](https://app.pluralsight.com/player?course=python-getting-started&author=bo-milanovich&name=python-开始 - m3&clip = 6&mode = live) –

我想通了这个问题。我错过了代码中的一个小细节。在add_creature()的定义,我不得不默认值分配给性别:

def add_creature(race, sex="male"): 
    creature = {"race": race, "creature_sex": sex} 
    creatures.append(creature) 

这是教程如何能够调用该函数只有在比赛的参数。一旦我添加了="male"代码就能按预期工作。