Python Forum
Printing out incidence values for Class Object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing out incidence values for Class Object
#1
Hello all,

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
Larz60+ write Mar-31-2024, 10:36 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
Character needs clothing arttributes, just like it has name and age attributs,
Reply
#3
Here is one possible way.

from random import choice, randint 

class Character:
    def __init__(self, clothing, gear):
        self.name = choice(('Charlie', 'Troy', 'Elaina', 'Brenda', 'Ralph'))
        self.age = randint(18, 35)
        self.clothing = clothing 
        self.gear = gear 


    def __str__(self):
        return f' Name: {self.name}\n Age: {self.age}\n Clothing: {self.clothing}\n Gear: {self.gear}'


class Clothing:
    def __init__(self):
        self.material = ('cotton', 'silk', 'leather')
        self.color = ('red', 'blue', 'green', 'black', 'white', 'yellow')
        self.top = f'{choice(self.color)} colored top made from {choice(self.material)}'
        self.pants = f'{choice(self.color)} colored pants made from {choice(self.material)}'
        self.belt = f'{choice(("rope", "leather", "cloth"))} belt'

    def __str__(self):
        return f'{self.top}, {self.pants}, {self.belt}'


class Gear:
    def __init__(self):
        self.material = ('copper', 'bronze', 'iron', 'steel')
        self.color = ('gold', 'silver', 'bronze')
        self.sword = f'{choice(self.color)} colored sword made from {choice(self.material)}'
        self.shield = f'{choice(self.color)} colored shield made from {choice(self.material)} with a {choice(('dragon', 'eagle', 'snake'))} emblem on it'

    def __str__(self):
        return f'{self.sword} and {self.shield}'

print(Character(Clothing(), Gear()))
Output
Output:
Name: Ralph Age: 33 Clothing: blue colored top made from cotton, white colored pants made from leather, cloth belt Gear: silver colored sword made from iron and bronze colored shield made from copper with a snake emblem on it
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
(Mar-31-2024, 08:44 PM)menator01 Wrote: Here is one possible way.

from random import choice, randint 

class Character:
    def __init__(self, clothing, gear):
        self.name = choice(('Charlie', 'Troy', 'Elaina', 'Brenda', 'Ralph'))
        self.age = randint(18, 35)
        self.clothing = clothing 
        self.gear = gear 


    def __str__(self):
        return f' Name: {self.name}\n Age: {self.age}\n Clothing: {self.clothing}\n Gear: {self.gear}'


class Clothing:
    def __init__(self):
        self.material = ('cotton', 'silk', 'leather')
        self.color = ('red', 'blue', 'green', 'black', 'white', 'yellow')
        self.top = f'{choice(self.color)} colored top made from {choice(self.material)}'
        self.pants = f'{choice(self.color)} colored pants made from {choice(self.material)}'
        self.belt = f'{choice(("rope", "leather", "cloth"))} belt'

    def __str__(self):
        return f'{self.top}, {self.pants}, {self.belt}'


class Gear:
    def __init__(self):
        self.material = ('copper', 'bronze', 'iron', 'steel')
        self.color = ('gold', 'silver', 'bronze')
        self.sword = f'{choice(self.color)} colored sword made from {choice(self.material)}'
        self.shield = f'{choice(self.color)} colored shield made from {choice(self.material)} with a {choice(('dragon', 'eagle', 'snake'))} emblem on it'

    def __str__(self):
        return f'{self.sword} and {self.shield}'

print(Character(Clothing(), Gear()))
Output
Output:
Name: Ralph Age: 33 Clothing: blue colored top made from cotton, white colored pants made from leather, cloth belt Gear: silver colored sword made from iron and bronze colored shield made from copper with a snake emblem on it

Excellent!

Many thanks Smile

SquiderDragon
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 538 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  Printing specific values out from a dictionary mcoliver88 6 1,443 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,660 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,062 Jan-19-2022, 08:33 AM
Last Post: menator01
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,917 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Printing Object Names JoeDainton123 5 2,789 May-09-2021, 10:33 PM
Last Post: deanhystad
  Printing x values from an csv file hobbyist 7 4,049 Mar-10-2021, 02:00 PM
Last Post: hobbyist
  AttributeError class object has no attribute list object scttfnch 5 3,494 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Python Class and Object Parameters JoeDainton123 5 2,918 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  Object already contains values when created Stef 4 2,406 Aug-20-2020, 09:42 AM
Last Post: Stef

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020