Mar-31-2024, 12:19 PM
Hello all,
new coder here.
I wanted to start by creating a character generation code. I created the following code:-
Example would be :-
I can see that Name and Age are being selected and output from
but nothing from class Clothing:
Thanks!
SquiderDragon
new coder here.
I wanted to start by creating a character generation code. I created the following code:-
import random class Character: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"Name: {self.name}\nAge: {self.age}\n{self.clothing}" class Clothing: def __init__(self): self.top = self.generate_clothing("top") self.lower_body = self.generate_clothing("lower body") self.leggings = self.generate_clothing("leggings") self.belt = self.generate_clothing("belt") self.sword = self.generate_clothing("sword") self.shield = self.generate_clothing("shield") self.undergear = self.generate_undergear() def generate_clothing(self, clothing_type): materials = ["cotton", "silk", "leather"] colors = ["red", "blue", "green", "black", "white", "yellow"] material = random.choice(materials) color = random.choice(colors) return f"{color} {material} {clothing_type}" def generate_undergear(self): materials = ["iron", "steel", "bronze"] colors = ["silver", "gold", "bronze"] material = random.choice(materials) color = random.choice(colors) return f"{color} {material} undergear (sword and shield)" def generate_random_name(): prefixes = ["Sir", "Lord", "Lady", "Princess", "Prince", "Master", "Mistress"] suffixes = ["blade", "fire", "ice", "thunder", "storm", "shadow", "light"] name = random.choice(prefixes) + " " + random.choice(suffixes) return name def generate_random_age(): return random.randint(18, 65) def generate_character(): name = generate_random_name() age = generate_random_age() return Character(name, age) def main(): character = generate_character() print("\nCharacter Details:") print(character) if __name__ == "__main__": main()running this returns:-
Output:Character Details:
Name: Master light
Age: 18
<__main__.Clothing object at 0x104c25e10>
my question hopefully is simple. How do I change or add to this code so the output would return the details from self.clothing = Clothing()? i.e. what is output as <__main__.Clothing object at 0x104c25e10>?Example would be :-
Output:Name: Master light
Age: 18
"Wearing leather top and carrying and steel sword and thunder shield"
What I mean is outputting the values that should be random generated from class Clothing:?I can see that Name and Age are being selected and output from
class Character: def __init__(self, name, age): self.name = name self.age = age
but nothing from class Clothing:
Thanks!
SquiderDragon