Python Forum

Full Version: where did i go wrong
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
so i had a text file which had data on seperate lines and i had to use the mod method to take out the desired values and convert it into a list(as the data is in consistent order) but it keeps throwing an error.Oh and the data is repeated after every 5 lines, like first is the id then name then earnings etc
f= open("my employees.txt")
emp_name = ['']*99999
hours_spent = [0]*99999
per_hours_salary = [0]*99999
index = 0
line = 0
while True:
    rough = f.readline()
    line += 1
    length = len(rough)-1
    if rough == '':
        break
    if line % 5 == 1:
        emp_name[index] = rough[:length]
        line += 1
    elif line % 5 == 2:
        per_hours_salary[index] = int(rough[:length])
        line += 1
    elif line & 5 == 3:
        hours_spent[index] = int(rough[:length])
        line += 1
    else:
        line += 1
    index += 1
emp_name[index+1:] = []
per_hours_salary[index+1:] = []
hours_spent[index+1:] = []
print(emp_name)
print(per_hours_salary)
print(hours_spent)
the output is:
['12344', '', '', '', '', '98123', '', '', '', '', '32145', '', '', '', '', '']
[0, 0, 0, 40, 0, 0, 0, 0, 34, 0, 0, 0, 0, 30, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
the data is present in the string(except the 3rd) but there are all the unnecessary bits)
where did i go wrong!
There are several errors. You are increasing line too often, so remove all the line += 1 except the one at line 9. Also you are increasing index too often. Move the index += 1 statement in the if between line 13 and 14 and remove it from line 24. Finally at line 19 I think & is a typo and you actually mean %.