1

This is my (shortened) code.

race1names = ["Jeff", "Paul", "Mark"]
race1odds = [5, 6, 7]

class Horse:
    def __init__(self, name, odds, age, weight):
        self.name = name
        self.odds = odds
        self.age = age
        self.weight = weight

def Horsecreate():
    hcount = 0
    horse = {}
    while hcount < 3:
        cname = race1names[hcount]
        codds = race1odds[hcount]
        cage = 3
        cweight = 3

        for i in range(0, 3):
            horse[i] = Horse(cname, codds, cage, cweight)

        hcount +=1

Horsecreate()

print(horse0.name)
print(horse1.name)

My Error is:

File "file.exe", line 26, in <module>
    print(horse0.name)
NameError: name 'horse0' is not defined

I tried a few things but no avail. To my knowledge this should work?

Update

After looking over both your answers I changed a few things with the code. New error.

Also, I didn't clarify my intent. I basically want to run this function, which will recursively pull various variables, adding them to a new class instance together as a new "horse actor", if that makes sense (I'm fairly new to coding).

In this case, I want it to create horse[0], who will be "Jeff", with odds of 5.

horse[1] who will have odds of 6

and, horse[2], who will have odds of 7

(from the two simple test lists "race1names" and "race1odds")

Ultimately I'd want to have multiple horses, all with their own independent values. This is just test scripting however, more complexity will be added at a later date.

The updated code is:

race1names = ["Jeff", "Paul", "Mark"]
race1odds = [5, 6, 7]

horse = {}

class Horse:
    def __init__(self, name, odds, age, weight):
        self.name = name
        self.odds = odds
        self.age = age
        self.weight = weight

def Horsecreate():
    for i in range(0, 2):
        cname = race1names[i]
        codds = race1odds[i]
        cage = 3
        cweight = 3
        horse[i] = Horse(cname, codds, cage, cweight)

        return horse


horse = Horsecreate()

print(horse[0].name)
print(horse[1].name)
print(horse[2].name)

With the error:

Jeff
Traceback (most recent call last):
  File "file", line 27, in <module>
    print(horse[1].name)
KeyError: 1

Which looks quite simple, but again, attempted a few things, none of which worked.

Notibly, "Jeff" is printed, showing it kind of works.


After removing the return, it now gives me:

Traceback (most recent call last):
  File "file", line 26, in <module>
    print(horse[0].name)
TypeError: 'NoneType' object is not subscriptable

Thank you, the super speedy help is much appreciated


In response to @gilch, I removed the reassignment and return. It now gives me the error:

print(horse[0].name)
KeyError: 0

As if it isn't assigned. Here's the current code:

race1names = ["Jeff", "Paul", "Mark"]
race1odds = [5, 6, 7]
global horse
horse = {}

class Horse:
    def __init__(self, name, odds, age, weight):
        self.name = name
        self.odds = odds
        self.age = age
        self.weight = weight

def Horsecreate():
    for i in range(0, 2):
        cname = race1names[i]
        codds = race1odds[i]
        cage = 3
        cweight = 3
        horse[i] = Horse(cname, codds, cage, cweight)


print(horse[0].name)
print(horse[1].name)
print(horse[2].name)
5
  • What is the expected behaviour of your program? Commented Jan 2, 2019 at 2:50
  • @DWuest Please see above Commented Jan 2, 2019 at 3:41
  • If you remove the return it returns None implicitly, so you should also remove the horse = reassignment so you keep the original global. Commented Jan 2, 2019 at 3:52
  • @gilch new error Commented Jan 2, 2019 at 3:59
  • 1
    Seriously, you do have to call the function if you expect it to execute. I wasn't suggesting you remove that part, just the reassignment horse =. Do try to think through these things. Try snippets of code in the Python console to see how it works. Insert print statements to check your assumptions. At this point I have to recommend you read a beginner textbook on Python, like Byte of Python (free online). I can't write the textbook for you. Commented Jan 2, 2019 at 4:38

1 Answer 1

4

Add global horse in Horsecreate and change the print to print(horse[0].name)

The horse variable is local to the Horsecreate function. Functions create local scope in Python. Assignments created inside the function are not visible outside of it. The global keyword makes it visible outside the function instead of a local. You could have also declared horse = {} outside the function instead.

And how is Python to know that the 0 is not just part of a new variable name when you say horse0? You have to include the [] subscripting operator the same as when you assigned it.


Your nested loops also don't make sense. You'll throw away all the horses made before the last while loop.


The updated version has a return inside the for loop. This immediately ends the loop, so you only get one loop and not three like you wanted. You don't need to return and reassign horse at all if it's a global. But if you do return it after the loop, it doesn't need to be a global. Do one or the other.

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

2 Comments

Thank you very much, please see above, new problem.
Removed the return, see above

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.