Basically, The user creates a creature by giving it a name, this name is then fed into the creation of the creature, where it is then given stats (This all works fine, even when making several creatures). However, I want to give the option to the user to check the stats of the creature they created through the 'check_stats()' function, they do this by inputting the slot number of the creature in their line-up, the code then finds the name of the creature through this, and calls the check_stat function using it. However this does not work as I receive the error:
AttributeError: 'str' object has no attribute 'check_stats'
If i create the creature manually in the code, like I write:
Bill = Pet('Bill')
It works perfectly and i can call the stats whenever. Its just when the user creates the class instance, it does not work.
This is my code (That is the problem):
def create_creature():
creature = Pet(name)
creature.create_stats()
print(" \n \n \n")
minions = {}
minion_value = {
1: None,
2: None,
3: None,
4: None,
5: None,
6: None
}
minion_slot = 0
for value in range(2):
name = input("Create a pet!\n> ")
minions[name] = create_creature()
minion_slot += 1
minion_value.update({minion_slot : name})
print("Your line-up")
for creature_ in minion_value:
print("{}: {}".format(creature_, minion_value[creature_]))
check_creature = int(input("Which pet would you like to check? (number)\n> "))
for creature_ in minion_value:
if check_creature == creature_:
(minion_value[check_creature]).check_stats()
This is what the output of this code looks like:
Create a pet!
> Bob
---Bob---
3 years old
5% healthy
66% happy
65% crazy
48% smarts
Create a pet!
> Bill
---Bill---
9 years old
100% healthy
35% happy
93% crazy
13% smarts
Your line-up
1: Bob
2: Bill
3: None
4: None
5: None
6: None
Which pet would you like to check? (number)
> 1
Traceback (most recent call last):
File "C:\Users\c10ld\Downloads\Test_class_animals.py", line 103, in <module>
(minion_value[check_creature]).check_stats()
AttributeError: 'str' object has no attribute 'check_stats'
This is what it should look like:
Create a pet!
> Bob
---Bob---
3 years old
5% healthy
66% happy
65% crazy
48% smarts
Create a pet!
> Bill
---Bill---
9 years old
100% healthy
35% happy
93% crazy
13% smarts
Your line-up
1: Bob
2: Bill
3: None
4: None
5: None
6: None
Which pet would you like to check? (number)
> 1
Your Pet:
---Bob---
3 years old
5% healthy
66% happy
65% crazy
48% smarts
Can anyone help me? Thank you! (Sorry if this is too long, its my first post and i wanted to make sure there was little to no confusion.)
EDIT: Here is the rest of the code, including the Pet class:
import random
class Animal(object):
def __init__(self, age, health, happiness):
self.age = 0
self.health = 0
self.happiness = 0
def get_age(self):
self.age = random.randint(8, 100)
def get_health(self):
self.health = random.randint(1, 100)
print(f"{self.health}% healthy")
def get_happiness(self):
self.happiness = random.randint(1, 100)
print(f"{self.happiness}% happy")
def age_up(self):
self.age += 1
print(f"{int(round(self.age, 0))} years old")
if self.age < 17:
self.age_up()
elif self.age >= 17:
print("died of age.")
class Pet(Animal):
def __init__(self, name):
self.name = name
self.craziness = 0
self.intelligence = 0
def pet_age(self):
self.get_age()
self.age = self.age / 7
print(f"{int(round(self.age, 0))} years old ")
def get_craziness(self):
self.craziness = random.randint(1,100)
print(f"{self.craziness}% crazy")
def get_intelligence(self):
self.intelligence = random.randint(1, 100)
print(f"{self.intelligence}% smarts")
def create_stats(self):
print(f"---{self.name}---")
self.pet_age()
self.get_health()
self.get_happiness()
self.get_craziness()
self.get_intelligence()
def check_stats(self):
print(f"---{self.name}---")
print(f"{int(round(self.age, 0))} years old")
print(f"{self.health}% healthy")
print(f"{self.happiness}% happy")
print(f"{self.craziness}% crazy")
print(f"{self.intelligence}% smarts")
I created an 'Animal' class as i was planning on creating more sub-classes, such as workers and warriors!