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:)

8
  • 3
    No, you don't need a new variable for each dog. You just need to store them in a list. Commented Jan 16, 2019 at 21:10
  • "i tried to put it into a for loop" I don't see a for loop in the code anywhere. Commented Jan 16, 2019 at 21:11
  • Why do you need a separate variable? Can use list of dictionary Commented Jan 16, 2019 at 21:11
  • Where are the other attribute values coming from? Are they random? Commented Jan 16, 2019 at 21:12
  • yes they are random Commented Jan 16, 2019 at 21:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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:
    ...

1 Comment

Thanks you, this was honestly useful forME

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.