Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
where did i go wrong
#1
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!
Reply
#2
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 %.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,573 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,017 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020