gulliver
gulliver

Reputation: 73

Unexpected traceback on text file in Python

I'm experiencing a "Traceback (most recent call last):" error when I try to write to a text file using this code. All three files already exist (although I believe they shouldn't have to - the code should create them if it cannot find them, should it not?), and they are all in the same directory as the .py file. I can't see my mistake - what have I done wrong?

import random 

forename="" 
surname=""
classno=0 
numberone=0 
numbertwo=0
correct=False
score=0
ops = ["+", "x", "-"] 



while forename == "" or forename.isnumeric():
    forename=input("What is your first name? ")
    if forename == "": 
        print("You have to enter your first name.")
    if forename.isnumeric() == True:
        print("Your name must contain letters.")

while surname == "" or surname.isnumeric():
    surname=input("What is your surname? ")
    if surname == "":
        print("You have to enter your name.")
    if surname.isnumeric() == True:
        print("Your name must contain letters.")

while classno not in [1,2,3]:
    while True:
        try:
            classno=int(input("What class are you in? "))
            break
        except ValueError: 
            print("That wasn't right. Please try again.")



for x in range(10): 
    operation=random.choice(ops) 

if operation == "-": 
    numberone=random.randint(0,10) 
    numbertwo=random.randint(0,numberone)

elif operation == "x":
    numberone=random.randint(0,12)
    numbertwo=random.randint(0,12) 

else:
    numberone=random.randint(0,100)
    numbertwo=random.randint(0,(100-numberone))

while True:
        try:
            answer=int(input("What is " + str(numberone) + str(operation) + str(numbertwo) + "? "))
            break 
        except ValueError: 
            print("Incorrect input. Please try again.")


if operation=="+":
    if answer==numberone+numbertwo:
        correct=True

elif operation=="-":
    if answer==numberone-numbertwo:
        correct=True

else:
    if answer==numberone*numbertwo:
        correct=True

if correct==True:
    print("Correct!")
    score=score+1

else:
    print("Wrong!")

correct = False


if classno == 1: 
    file1=open("class1.txt", "a")
    file1.write(forename,surname,score,"\n")
    file1.close()

elif classno == 2: 
    file2=open("class2.txt", "a")
    file2.write(forename,surname,score,"\n")
    file2.close()

else:
    file3=open("class.text", "a")
    file3.write(forename,surname,score,"\n")
    file3.close()


print("You scored",score,"out of 10.")

EDIT: this is what I'm seeing after the questions: enter image description here

Upvotes: 1

Views: 127

Answers (1)

rafaelc
rafaelc

Reputation: 59274

The problem is probably when you try to write to the file, in this line:

file2.write(forename,surname,score,"\n")

The write method receives only one argument. Build a string as pass it as argument.

For example:

line_to_be_written = str(forename) + " " + str(surname) + " : " + str(score) + "\n"
file2.write(line_to_be_written)

The above way is simpler to understand, but as recommended by Bhargav, you can use format as:

line_to_be_written = '{0} {1} {2} {3}'.format(forename, surname, score, '\n')

or even

line_to_be_written = '%s %s %s %s' % (forename, surname, score, '\n')

Upvotes: 2

Related Questions