jeudi 15 avril 2021

Sum up random but not defined numbers

I am trying to find a way to add up just the randomly generated numbers, but not the defined numbers from my code given below.

import random
class Pet:
    def __init__(self, name, species, age, tricks, fleas=None):
        if fleas is None:
            fleas = random.randint(0,10)
        self.name = name
        self.species = species
        self.age = age
        self.tricks = tricks
        self.fleas = fleas
    def __str__(self):
        return f"{self.name} is {self.age} year old {self.species} who can {self.tricks} with {self.fleas} fleas"

Snaps = Pet("Snaps", "Dog", 8, "fetch and roll over")
Charlie = Pet("Charlie", "Cat", 6, "roll over and play dead")
Pabu = Pet("Pabu", "Ferret", 4, "shake hands and open jars")

print(Snaps)
print(Charlie)
print(Pabu)
def randnums():
    total=0
    for count in range(0,10):
        number = random.randint(0,10)
        total+=number
    print(total)

randnums()

Right now it seems to be adding up all the numbers, but I just need the total number of randomly generated fleas. Also, is there a way to make the fleas a class attribute while still making them a random number and showing up in the print? Thanks!




Aucun commentaire:

Enregistrer un commentaire