-4

This is a maths quiz consisting of random questions. At the end of the quiz a score is displayed, and then I try to put the result and name of student in a file and an error message pops up:

import random
import time

counter = 0

#I think the problem is around here?
score = int("0")
count = 0

function = ['+', 'x', '-']

# Introducing quiz
print('Welcome To The Arithmetic Quiz!')
time.sleep(2)

name = input('Please enter you name. ')
time.sleep(1)

print('Thanks', name, '. Let\'s Get Started!')
time.sleep(1)

while counter < 10:
questions.
    firstnumber = random.randint(0, 12)
    secondnumber = random.randint(0, 6)
    operator = random.choice(function)

    question = print(firstnumber, operator, secondnumber, '=')

    userAnswer = input('Answer:')

    if operator == '+':
        count = firstnumber + secondnumber
        if count == int(userAnswer):
            print('Correct!')
            score = score+1
        else:
            print('Incorrect')
    elif operator== 'x':
        count = firstnumber*secondnumber
        if count == int (userAnswer):
            print('Correct!')
            score = score+1
        else:
            print('Incorrect')
    elif operator== '-':
        count = firstnumber - secondnumber
        if count == int(userAnswer):
            print('Correct!')
            score = score + 1
        else:
            print('Incorrect')
    counter += 1

    print("Your quiz is over!")
    print("You scored", score, "/10")
    what_class = input("Please enter your class number: ")
    classe = open("what_class.txt", "wt")
    type(classe)
    classe.write(name + score)
    classe.close()

Then this error message comes up:

Traceback (most recent call last):
  File "C:/4/gcse maths.py", line 61, in <module>
    classe.write(name+score)
TypeError: Can't convert 'int' object to str implicitly
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HELP.ME
  • 1
  • 1
  • 1
  • 2

3 Answers3

2

Right, because strings and ints can't be concatenated, it doesn't make sense to do!

Let's say we have:

oneString = 'one'
twoInt = 2

then what type is

oneString + twoInt

?

Is it a str, or is it an int?

Because of this, you can explicitly parse the int to a str by the str() builtin:

result = oneString + str(twoInt)
print(result)
# printed result is 'one2'

But be mindful of the reciprocal of this situation, which would be to convert oneString to an int. You will get a ValueError. Please see the below:

result = int(oneString) + twoInt
print(result)
# raises a ValueError since 'one' can not be converted to an int
the_constant
  • 681
  • 4
  • 11
0

The code is unable to add the string 'name' to the number 'score'. Try using the function str() to convert score to a string (you might want to add a space in there too). Take a look at this question Converting integer to string in Python?

Community
  • 1
  • 1
Dweeberly
  • 4,668
  • 2
  • 22
  • 41
0

When writing to a file you can only write strings, not integers. To solve that, the integer needs to be converted to a string. This can be done using the str() function - more information here.

classe.write(name + str(score) + "\n")

\n is for a new line, otherwise each name and score will be on the same line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom
  • 918
  • 6
  • 19