0

I dont know how to create a new variable for every new name from a list using a for loop.

As im new to this coding thing, I was doing a little school project and trying to shorten my code and got into a little problem of a long list I had. So i tried to put it into a for loop and so that for every different name(weird given dog names) in a list i could get a new variable for it but just got stuck. Is there anyway anyone could come up with a solution?

import random

class info():

    def __init__(self, name, exercise, intel, friend, drool):
        self.name = name
        self.exercise = exercise
        self.intel = intel
        self.friend = friend
        self.drool = drool


    def __repr__(self):
        return "\nName : {}\na-Exercise : {}\nb-Intelligence : {}\nc-Friendliness : {}\nd-Drool : {}".format(
            self.name, self.exercise, self.intel, self.friend, self.drool)

dog_names = ["Annie the Afgan Hound", "Bertie the Boxer", "Betty the Borzoi", "Charlie the Chihuahua", "Chaz the Cocker Spaniel", "Donald the Dalmatian", "Dottie the Doberman", "Fern the Fox Terrier", "Frank the French Bulldog", "George the Great Dane", "Gertie the Greyhound", "Harry the Harrier", "Ian the Irish Wolfhound", "Juno the Jack Russell", "Keith the Kerry Blue", "Larry the Labrador", "Marge the Maltese", "Max the Mutt", "Nutty the Newfoundland", "Olive the Old English Sheepdog", "Peter the Pug", "Poppy the Pekingese", "Rosie the Rottweiler", "Ruby the Retriever", "Sam the Springer Spaniel", "Sukie the Saluki", "Vernon the Vizsla", "Whilma the West Highland Terrier", "William the Whippet", "Yolande the Yorkshire Terrier"]

#This is what i want to shorten

a = info("Annie the Afgan Hound", random.randint(1, 5), random.randint(1, 100), random.randint(1, 10), random.randint(1, 10))#1

b = info("Bertie the Boxer", random.randint(1, 5), random.randint(1, 100), random.randint(1, 10), random.randint(1, 10))
#etc

I would like to create a "card" for each name; so that every time this code is ran, there is a new variable for each name. Eg...

a = Name:Annie the Afgan Hound |
    Exersice:10 |
    Intelligence:34 |
    Friendliness:4 |
    Drool:2

b = Name:Bertie the Boxer |
    Exersice:7 |
    Intelligence:87 |
    Friendliness:9 |
    Drool:10

New to this, so any help will be appreciated:)

Prune
  • 76,765
  • 14
  • 60
  • 81

2 Answers2

1

You need something like this:

for dog_name in dog_names:
    new_dog_info = info(
        name = dog_name,
        exercise = randint(0, 100),
        ...

If this is a homework assignment you probably don't want me to write it all out.

Brad Fallon
  • 169
  • 1
  • 7
0

You'll almost certainly want to put them into a list:

kennel = []
for name in dog_names:
    kennel.append(info(name,
                       random.randint(1, 5), 
                       random.randint(1, 100),
                       random.randint(1, 10),
                       random.randint(1, 10)))

When you finish this loop, you'll have all your dog info objects in this list, and you can conveniently iterate through that list.

for dog in kennel:
    ...
Prune
  • 76,765
  • 14
  • 60
  • 81