import random
line_count = 0
for line in file:
line_count = line_count + 1
line_count = line_count - 1
print(line_count)
randomLine = random.randrange(0, line_count)
print(randomLine)
lines = file.readlines()
print(lines[randomLine])
I checked what is needed with trying to cancel the index error code, I reduced the line value by 1 so it wont try and print a non existent line. I do not know what is wrong with the code.
This is the input file
Line 16 - 5x - 5 = 20
Line 17 - 5
Line 18 - 1x + 5 = 10
Line 19 - 5
Error Message
Traceback (most recent call last):
File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 252, in <module>
mainscreen()
File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 242, in mainscreen
main_menu()
File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 210, in main_menu
quiz1()
File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 158, in quiz1
quiz2()
File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 121, in quiz2
print(lines[randomLine])
IndexError: list index out of range