Before I even try to explain this, here is the code I'm working with currently:
currentLine = 1
try:
with open(filename, 'r') as f:
for line in f:
if currentLine == 1:
tempName = line
if currentLine == 2:
tempColour = line
if currentLine == 3:
tempAge = line
if currentLine == 4:
tempWeight = line
currentLine = 1
tempSheep = Sheep(tempName, tempColour, tempAge, tempWeight)
sheepList.append(tempSheep)
currentLine = currentLine + 1
except:
print("file is in an invalid format")
continue
else:
break
The goal of the code is to read 4 line from a file (name, colour, age, and weight) and put them into a Sheep object. This needs to be done in a loop, as there are anywhere from 2 - 10 sheep per file. The code mostly works, in that it reads lines and puts them in the class, but it doesn't read the right lines. When I print out all the sheep, every sheep as the same name, "bob", which is the name of the first sheep in the file and is the first line. Beyond that, it actually works, but it totally ignores the name variable, just putting bob in it. I end up with this mess of sheeps named bob.
for example, a sample output looks like this:
Name: bob
Colour: blue
age: 5
weight: 50
name: bob
Colour: tina
age: red
Weight: 7
name: bob
colour: 75
age: shirley
Weight: green
in case it isn't obvious, its offsetting everything by ignoring the name. I hope this was explained well enough, if you need further explanation I can try to put in some more samples.
why are my program be bad?