我制作了一个动物发出声音的简单程序。但是,它在shell中的每一行之后都不打印。在打印文本的编码中是否应该更改某些内容?或者我是否必须删除任何文本?
以下是一个例子:
我是一只狗
无
汪!汪汪!
无
我的代码是:
import pets
def main():
print "Choose number to hear what sound the animal makes."
print "1. Enter choice"
print "2. Dog"
print "3. Cat"
print "4. Bird"
print "5. Quit program"
choice = input("Choice: ")
while choice != 5:
if choice == 1:
species = raw_input("Input an animal: ")
userchoice = pets.Pet(species)
print userchoice.show_species()
print userchoice.make_sound()
choice = input("Choice: ")
elif choice == 2:
userchoice = pets.Dog()
print userchoice.show_species()
print userchoice.make_sound()
choice = input("Choice: ")
elif choice == 3:
userchoice = pets.Cat()
print userchoice.show_species()
print userchoice.make_sound()
choice = input("Choice: ")
elif choice == 4:
userchoice = pets.Bird()
print userchoice.show_species()
print userchoice.make_sound()
choice = input("Choice: ")
else:
print "Error"
choice = input("Choice: ")
main()
导入的模块是
class Pet:
def __init__(self,species):
self.__species = species
def show_species(self):
print "I am a ", self.__species
def make_sound(self):
print "I do not make a sound."
class Dog(Pet):
def __init__(self):
Pet.__init__(self, "Dog")
def make_sound(self):
print "Woof! Woof!"
class Bird(Pet):
def __init__(self):
Pet.__init__(self, "Bird")
def make_sound(self):
print "Chirp! Chirp!"
class Cat(Pet):
def __init__(self):
Pet.__init__(self, "Cat")
def make_sound(self):
print "Meow! Meow!"
答案 0 :(得分:1)
您在此处make_sound
打印返回值:print userchoice.make_sound()
。由于make_sound
没有明确指定返回值,因此默认情况下会返回None
,这是您在输出中看到的内容。
您可以通过不打印返回值或更改make_sound
来返回声音而不是打印声音来解决问题。
答案 1 :(得分:0)
None
。由于您始终打印返回值但从不返回任何内容,因此始终打印None
。