Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试使用随机数为类模块生成结果_Python - Fatal编程技术网

Python 尝试使用随机数为类模块生成结果

Python 尝试使用随机数为类模块生成结果,python,Python,我无法返回_情绪字段为动物对象生成随机情绪。我不知道如何让它工作,所以我一直在尝试在名为animals.py的程序中定义它 class Animal: # The __init__ method initializes the attributes def __init__(self, name, mood, type): self.__name = name self.__mood = mood self.__animal_t

我无法返回_情绪字段为动物对象生成随机情绪。我不知道如何让它工作,所以我一直在尝试在名为animals.py的程序中定义它

class Animal:

    # The __init__ method initializes the attributes

    def __init__(self, name, mood, type):
        self.__name = name
        self.__mood = mood
        self.__animal_type = type

    def _animal_type(self, type):
        self.__animal_type = type

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

    def __mood(self, mood):
        for i in range():
            if random.randint(1, 3) == 1:
                self.__mood = 'happy'
            if random.randint(1, 3) == 2:
                self.__mood = 'hungry'
            if random.randint(1, 3) == 3:
                self.__mood = 'sleepy'
            else:
                self.__mood = 'happy'

    def get_animal_type(self):
        return self.__animal_type

    def get_name(self):
        return self.__name

    def check_mood(self):
        return self.__mood
我有两个程序:anims.py和animalgenerator.py

The animal generator asks for user input and produces a list that looks like:
What    type    of  animal  would   you like    to  create? Truman
What    is  the animal's    name?   Tiger
Would   you like    to  add more animals    (y/n)?  n

Animal  List
-----------
Tiger   the Truman  is  hungry
到目前为止,我的计划已经奏效,但它不会产生情绪。 __情绪是动物对象的隐藏属性

check_mood:此方法应生成一个介于1之间的随机数 三,。 随机数将用于设置三种情绪之一:

如果数字为1,则_mood字段应设置为“happy”值

如果数字为2,则_mood字段应设置为“饥饿”值

如果数字为3,则_mood字段应设置为“sleepy”值

最后,此方法应返回_mood字段的值

这是我对动物的看法

class Animal:

    # The __init__ method initializes the attributes

    def __init__(self, name, mood, type):
        self.__name = name
        self.__mood = mood
        self.__animal_type = type

    def _animal_type(self, type):
        self.__animal_type = type

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

    def __mood(self, mood):
        for i in range():
            if random.randint(1, 3) == 1:
                self.__mood = 'happy'
            if random.randint(1, 3) == 2:
                self.__mood = 'hungry'
            if random.randint(1, 3) == 3:
                self.__mood = 'sleepy'
            else:
                self.__mood = 'happy'

    def get_animal_type(self):
        return self.__animal_type

    def get_name(self):
        return self.__name

    def check_mood(self):
        return self.__mood
这是我为animalgenerator.py准备的

# This program tests the Animal class.

import animals

print("Welcome to the animal generator!")
print("This program creates Animal objects.")

def main():
    # Get the animal data
    animal_list = []
    find_info = True
    while(find_info):
        _animal_type = input('\nWhat type of animal would you like to create? ')
        __name = input('What is the animals name? ')
        more_animals = input('Would you like to add more animals (y/n)? ')
        if (more_animals != 'y'):
            find_info = False

        # Create an instance of animal class
        animal_list.append(animals.Animal(_animal_type, __name, __mood))

    animal = animals.Animal(_animal_type, __name, __mood)

    # Display the data that was entered.
    print('\nAnimal List\n')
    print('------------- \n')
    for animal in animal_list:
        print('' + animal.get_animal_type() + ' the ' + animal.get_name() + ' is ' + animal.check_mood() + '\n')


# Call the main function
main()

有几点想法:首先,你对范围内的i的情绪:但范围至少需要1个参数。我想你可能根本不想这样,因为我看不出有什么理由在那里循环

其次,您可能不需要为每次检查生成新的随机数。如果你一次生成从1到3的随机整数,看看它是1、2还是3,你应该能够设置你想要的情绪

第三,检查\u-mood从不调用\u-mood来生成和设置新的情绪。还有,我读你的作业的方式,呃,要求,你应该在check\u mood中生成随机数,然后将其传递给\u\u mood,而不是在check\u mood中生成


第四,可能比上面的许多内容更重要,特别是第三点,u mood不能同时是方法名和属性名。也许你不想让“心情”成为一种方法,而只是把它的主体放在“心情”中。

我相信这种方法可以优雅地写成1-2行:

def __setmood(self):
    self.__mood = ('happy', 'hungry', 'sleepy')[random.randint(0, 2)]
    return self.__mood
但除此之外,我认为不应该对方法和实例变量使用相同的名称。当您执行self.\u mood='happy'之类的赋值时,实际上覆盖了对象方法的绑定。换句话说,你不能再叫self.\u mood方法了,即使是在课堂上

例如,以下代码将引发TypeError“str”对象不可调用: