1

I am trying to write a quiz program that saves and reads from text files, storing the users details in a unique text file for each user. Each user file is saved in the format:

name, age, (line 1)
username (line 2)
password (line 3)

I currently am getting the error 'IndexError: list index out of range' whilst attempting to read a two lines from a file so the program can decide whether the user has entered the correct login details. I understand that the error is due to the program not seeing line three but I cannot understand why.

import sys

def signUp ():
 name=input("Please enter your first name and last name. \n- ")
 name = name.lower()
 username=name[0:3]
 age= input("Please input your age. \n- ")
 username = username + age
 password = input("Please input a password. \n- ")
 print ("Your username is "+username+" and your password is " +password)
 userfile = open(name+".txt", "r")
 userfile.write(name+", "+age+"\n") 
 userfile.write(username+"\n")
 userfile.write(password+"\n")
 userfile.close()
 return name, age, username, password


def login():
 logname = input("Please enter your first name and last name. \n- ")
 logname = logname.lower()
 loginFile = open (logname+".txt", "r")
 inputuname = input ("Enter your username. \n- ")
 inputpword = input("Enter your password. \n- ")
 username = loginFile.readlines(1)
 password = loginFile.readlines(2)
 print (username)
 print (password)
 loginFile.close()
 if inputuname == username[1].strip("\n") and inputpword == 
 password[2].strip("\n"):
    print("success") 
    quiz()
 else:
    print("invalid")



def main():
 valid = False
 print ("1. Login")
 print ("2. Create Account")
 print ("3. Exit Program")
 while valid != True:
     choice =input("Enter an Option: ")

    if choice == "1":
        login()
    elif choice == ("2"):
        user = signUp()
    elif choice== ("3"):
        valid = True
    else:
        print("not a valid option")


def quiz():
 score = 0
 topic = input ("Please choose the topic for the quiz. The topics available 
 are: \n- Computer Science \n- History")
 difficulty = input("Please choose the diffilculty. The difficulties 
 available are: \n- Easy \n- Medium \n- Hard")
 questions = open(topic+"questions.txt", "r")

  for i in range(0,4):
    questions = open(topic+" "+difficulty+".txt", "r")
    question = questions.readline(i)
    print(question)
    answers = open (topic+" "+difficulty+" answers.txt", "r")
    answer = answers.readline(i)
    print(answer)
    userAns = input()
    questions.close
    answers.close
    if userAns == answer:
        score = score + 1
  return score

 main()
halfer
  • 19,824
  • 17
  • 99
  • 186
Smc_
  • 23
  • 2

1 Answers1

2

You should use with open(....) as name: for file operations.

When writing to a file you should use a for append / r+ for read+write, or w to recreate it - not 'r' that is for reading only.

As for your login():

def login():
    logname = input("Please enter your first name and last name. \n- ")
    logname = logname.lower()
    inputuname = input("Enter your username. \n- ")
    inputpword = input("Enter your password. \n- ")
    with open(logname + ".txt", "r") as loginFile:
        loginfile.readline()            # skip name + age line
        username = loginFile.readline() # read a single line
        password = loginFile.readline() # read a single line

    print(username)
    print(password)

    if inputuname == username.strip("\n") and inputpword == password.strip("\n"):
        print("success")          
        quiz()
    else:
        print("invalid")

File-Doku: reading-and-writing-files

If the file does not exist your code will crash, see How do I check whether a file exists using Python?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69