Traceback(最近一次调用最后一次)错误:

时间:2015-05-07 03:16:29

标签: python

请帮帮我,我无法跑步 得到错误:

Error: File "C:/Python34/pyt.py", line 6, in <module>  class Animal1: File "C:/Python34/pyt.py", line 30, in Animal1     cat = Animal1("cat",7) NameError: name 'Animal1' is not defined

代码:

import random
import sys
import os


class Animal1:
    __name = ""
    __age = 0

    def __init__(self,name,age):
        self.__nae = name
        self.__age = age

    def set_name(self,__name):
        self.__name = __name

    def get_name(self):
        return self.__name

    def set_age(self,__age):
        self.__age = age

    def get_age(self):
        self.__age


    def toPrint(self):
        return "Animal is {} and age is {}"

    cat = Animal1("cat",7)
    print(cat.toPrint())

1 个答案:

答案 0 :(得分:0)

我不知道你的代码是什么样的,但它应该是这样的:

class Animal1(object):

    def __init__(self, name, age):
        self.__name = name
        self.__age = age

    def set_name(self,__name):
        self.__name = __name

    def get_name(self):
        return self.__name

    def set_age(self,__age):
        self.__age = age

    def get_age(self):
        self.__age

    def toPrint(self):
        return "Animal is %s and age is %s" % (self.__name, self.__age)

cat = Animal1("cat", 7)
print(cat.toPrint())