我是Python的新手,对于如何让每只宠物打印出随机数目的跳蚤我有点迷茫。当我运行这个程序时,三只宠物的跳蚤数量都是一样的。你知道吗
import random
class pet :
#how the pets attributes will be displayed
def __init__(animal, type, name, age, flees):
self.type = type
self.name = name
self.age = age
self.flees = flees
#empty list for adding tricks
animal.tricks = []
def addFleas(self):
fleaCount = random.randint(0,10)
self.flees = fleaCount
def addTrick(self, trick):
self.tricks.append(trick)
def petAge(self):
return self.age
def printInfo(self):
print(f"Pet type : {self.type} \nPet name : {self.name}\nPet age : {self.age}\nPet fleas : {self.fleaCount}")
print("Tricks :")
for i in range(len(self.tricks)):
print("",self.tricks[i])
# main program
#dog1 information
dog1 = pet("Dog","Max",10, flees)
dog1.addTrick("Stay and Bark")
dog1.addFleas()
dog1.printInfo()
#dog2 information
dog2 = pet("Dog","Lily",8)
dog2.addTrick("Play Dead and Fetch")
dog2.printInfo()
#cat1 information
cat1 = pet("Cat","Mittens",11)
cat1.addTrick("Sit and High Five")
cat1.printInfo()
添加一些羊毛组件到你的宠物对象!你知道吗
例如,在初始化中:
然后创建一个函数(就像addTrick一样)为你的宠物分配跳蚤数。你知道吗
然后在main函数中,调用add Flees
希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐