简体   繁体   English

如果输入狗,猫或鸟,我如何让我的程序显示动物发出的噪音

[英]How do i get my program to display a noise that the animal makes if the input a dog, cat or bird

name = input("What is your pet's name?")
species = input("What type of pet is it?")
age = input("How old is your pet?")

class Pet:
    def __init__(self, name, species, age, noise):
        self.name = name
        self.species = species
        self.age = age
        self.noise = noise

    def setNoise(self, noise):
            self.noise = "WOOF"
        elif species == cat:
            self.noise = "MEOW!"
        elif species == bird:
            self.noise = "CAWW!"
        else:
            self.noise = "Animal Noise"

    def get_type(self):
        print("Pet")

    def toString(self):
        return"{} is a {} and is {} years old".format(self.name,self.species,self.age,self.noise)

myPet = Pet(name,species,age,noise)
print(noise, myPet.toString())

i cannot figure out how to make my program display a noise when i type a certain animal in it, such as a dog, cat, or bird. 当我在其中键入某些动物(如狗,猫或鸟)时,我不知道如何使程序显示噪音。 any help would be amazing. 任何帮助都将是惊人的。 I keep running into the error that noise is not defined. 我不断遇到未定义噪声的错误。 also when i try to use my species input to check if it equals dog it errors out. 当我尝试使用我的物种输入来检查它是否等于狗时,也会出错。

I think this might be what you're trying to get. 我认为这可能就是您想要得到的。 You used the cat , bird , etc. variables without defining them. 您使用了catbird等变量,但未定义它们。 I assume these should be strings. 我认为这些应该是字符串。 I also got rid of the setNoise function which didn't seem to have a purpose. 我也摆脱了似乎没有目的的setNoise函数。 Since the noise is based on the species, I set it at __init__ . 由于噪声是基于物种的,因此将其设置为__init__ Python also has a special method called __str__ . Python还具有一种称为__str__的特殊方法。 If you set this, then when I call print(myPet) it will print the return value of myPet.__str__() . 如果设置了此项,那么当我调用print(myPet) ,它将打印myPet.__str__()的返回值。 This is better to use than defining your own string method because it gets called automatically when printing. 这比定义自己的字符串方法要好用,因为在打印时会自动调用它。

name = input("What is your pet's name?")
species = input("What type of pet is it?")
age = input("How old is your pet?")
# you might want to set age = int(age) if age is, e.g., an integer number of years

class Pet:
    def __init__(self, name, species, age):
        self.name = name
        self.species = species
        self.age = age

        if species == "dog":
            self.noise = "WOOF"
        elif species == "cat":
            self.noise = "MEOW!"
        elif species == "bird":
            self.noise = "CAWW!"
        else:
            self.noise = "Animal Noise"

    def get_type(self):
        print("Pet")

    def __str__(self):
        return"{} is a {} and is {} years old".format(self.name,self.species,self.age,self.noise)

myPet = Pet(name, species, age)
print(myPet.noise, myPet)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将波形文件转换为 128x128 频谱图? 我正在尝试从 2 个文件夹的音频创建数据集:cat audio( label 1)/dog audio(2) - How do I convert a wave file to a 128x128 spectogram? I am trying to create a dataset from audio from 2 folders: cat audio( label 1)/dog audio(2) 如果密码在字典和键值对中,我如何解决此程序,以便使输入正常工作 - How do i fix this program so it makes the input work if the password is in the dictionary and key value pair 当我单击空格 Pygame 时,如何让我的鸟继续跳跃? - How Do I make My Bird Keep Jumping When I click Space Pygame? 我的程序创建了一个新文件,但名称错误。 我该如何解决? - My program makes a new file but with the wrong name. How do I fix this? Python-如何让我的程序反复要求输入,但仍存储其他输入? - Python - How do I get my program to ask for an input repeatedly but still store the other inputs? 猫狗分类 CNN 中的不兼容输入层大小错误 model - incompatible input layer size error in a Cat Dog Classification CNN model 如果输入无效输入,如何让我的python程序退出并显示错误消息? - how do i get my python program to exit and show an error message if an invalid input is entered? 如何在我的程序中显示中心的圆圈 - How do I display the circles in the center in my program 如何将 pysimplegui 输入值返回到我的程序? - How do I return the pysimplegui input value to my program? Pygame - 来自 Clear Code 的 Flappy Bird - 如何保存高分以供下次打开游戏并显示时使用? - Pygame - Flappy Bird from Clear Code - How do I save the high score for the next time I open the game and display it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM