【问题标题】:Python local variable referenced before assignment赋值前引用的 Python 局部变量
【发布时间】:2016-04-26 23:33:57
【问题描述】:

我试图找出我的代码在这里继续做错的地方。我已经尝试通过删除 # The main 函数之后的所有内容来更改第二部分。想法?

我收到以下错误

Traceback(最近一次调用最后一次):文件 “文件”,行 1、在导入petspgm文件 “文件”,行 39、在main()文件中 “文件”,行 34, in main print('宠物名:', pets.get_pet_name) builtins.UnboundLocalError:之前引用的局部变量“宠物” 作业——

import petspgm

def main():
    #Get a list of pet objects.
    pets = make_list()

    #Display the data in the list.
    print('Here is the data you entered: ')
    display_list(pets)

#This function gets data from the user for 3 pets. It returns a list of pet
#objects containing the data.
def make_list():
    #Create an
    pet_list = []

    #Add three pet objects to the list
    print('Enter the data for 3 pets.')
    for count in range(1, 3):
        #Get the pet data
        print("Pet number " + str(count) + ":")
        pet_name = input("Enter your pet's name: ")
        pet_type = input("Enter the type of pet: ")
        pet_age = float(input("Enter the pet's age: "))

        #Create a new PetData object in memory and assign it to the pet variable.
        pet = petspgm.PetData(pet_name, pet_type, pet_age)

        #Add the object to the list.
        pet_list.append(pet)

        #Return the list.
        return pet_list

#This function accepts a list containing PetData objects as an argument and 

def pet_list():

    #displays the data stored in each object.
    display_list(pet_list)
    for item in pet_list:
        print("Pet's name is: " + item.get_pet_name())
        print("Pet's type is : " + item.get_pet_type())
        print("Pet's age is : " + item.get_pet__age())

#Call the main function
main()

这是代码的另一半:

class PetData(object):
    def __init__(self, name, animal_type, age):
        self.__pet_name = pet_name
        self.__pet_type = pet_type
        self.__pet_age = pet_age

    def set_name(self, name):
        self.__pet_name = pet_name

    def set_type(self, animal_type):
        self.__pet_type = pet_type

    def set_age(self, age):
        self.__pet_age = pet_age

    def get_name(self):
        return self.__pet_name

    def get_animal_type(self):
        return self.__pet_type

    def get_age(self):
        return self.__pet_age


# The main function
def main():
    # Prompt user to enter name, type, and age of pet
    name = input('What is the name of the pet: ')
    animal_type = input('What type of pet is it: ')
    age = int(input('How old is your pet: '))
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_pet_name)
    print('Animal Type: ', pets.get_pet_type)
    print('Age: ', pets.get_pet_age)
    pets = Pet(name, animal_type, age)

main()

【问题讨论】:

  • 请提供更多细节。你的代码出了什么问题?发生了一些你不希望发生的事情吗?你期望发生的事情没有发生吗?具体stackoverflow.com/help/how-to-ask
  • 回溯(最近一次调用最后):文件“C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py”,第 1 行,在 中导入 petspgm 文件“ C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py”,第 39 行,在 main() 文件“C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm .py",第 34 行,在主打印中('Pet Name: ', pets.get_pet_name) builtins.UnboundLocalError: local variable 'pets' referenced before assignment
  • 请使用该数据更新您的原始问题

标签: python


【解决方案1】:

主要:

print('This will be added to the records. ')
print('Here is the data you entered:')
print('Pet Name: ', pets.get_pet_name)
print('Animal Type: ', pets.get_pet_type)
print('Age: ', pets.get_pet_age)
pets = Pet(name, animal_type, age)

在实际初始化/声明它之前打印宠物值。我想你想要:

    pets = Pet(name, animal_type, age)
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_pet_name)
    print('Animal Type: ', pets.get_pet_type)
    print('Age: ', pets.get_pet_age)

【讨论】:

  • 现在我的错误是给我回溯(最近一次调用最后一次):文件“C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py”,第 1 行,在 导入 petspgm 文件“C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py”,第 40 行,在 main() 文件“C:\Users\BakerFamily\Documents\Vince\ LCC\CITP 110\petspgm.py",第 32 行,在 main pets = Pet(name, animal_type, age) builtins.NameError: name 'Pet' is not defined
  • @bakervin730 查看文件以及您定义的类。为什么你认为Pet 找不到?
  • @viraptor 是不是因为之前是Petdata,而我忘记了其中的数据?
  • @viraptor 解决了这个问题,但另一个问题已经曝光......... Here is the data you entered: Traceback (most recent call last): File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 46, in <module> main() File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 9, in <module> display_list(pets) builtins.NameError: name 'display_list' is not defined
  • display_list(pet_list) 不起作用,因为函数 display_list 不是内置函数,需要您自己定义。也许只是打印 pet_list 代替?
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
相关资源
最近更新 更多