I am new in Python OOP and I am trying to understand one line of this Code (This is just a part of the whole Code)
I am trying to understand what "pet.name" in the methode "whichone" do. The parameter 'petlist' in whichone can be empty or a list with strings. The Parameter 'name' has just one string.
Can someone explain me what "pet.name" actually do?
from random import randrange
class Pet():
boredom_decrement = 4
hunger_decrement = 6
boredom_threshold = 5
hunger_threshold = 10
sounds = ['Mrrp']
def __init__(self, name = "Kitty"):
self.name = name
self.hunger = randrange(self.hunger_threshold)
self.boredom = randrange(self.boredom_threshold)
self.sounds = self.sounds[:]
def whichone(petlist, name):
for pet in petlist:
if pet.name == name:
return pet
return None # no pet matched
def play():
animals = []
option = ""
base_prompt = """
Quit
Choice: """
feedback = ""
while True:
action = input(feedback + "\n" + base_prompt)
feedback = ""
words = action.split()
if len(words) > 0:
command = words[0]
else:
command = None
if command == "Quit":
print("Exiting...")
return
elif command == "Adopt" and len(words) > 1:
if whichone(animals, words[1]):
feedback += "You already have a pet with that name\n"
else:
animals.append(Pet(words[1]))
play()
petlistmust be a list ofPetwhichonewith 1st argument beinglistofstrs and observe what would happen.