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)
horse =reassignment so you keep the original global.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.