-4

Its a random maths question that returns an error that I didn't expect. Please help! The error is: print('Your score was: '+int(score)) TypeError: Can't convert 'int' object to str implicitly

import random


count=0


while count<10:
    numb1=random.randint(1,12)
    numb2=random.randint(1,12)
    ops=[' add ',' times ',' takeaway ']
    ops2=random.choice(ops)
    question=str(numb1)+''.join(ops2)+str(numb2)
    print(question)
    ans=int(input('Answer: '))
    count=count+1
score=0
if ans== numb1+numb2 or numb1-numb2 or numb1*numb2:
    score=score+1

print('Your score was: '+score)
George Baker
  • 91
  • 1
  • 3
  • 13

2 Answers2

1

This line is incorrect. One cannot add an int and a str.

print('Your score was: '+score)

Use one of the following:

print('Your score was:', score)
print('Your score was: '+str(score))
print('Your score was: %d'%(score))
print('Your score was: {}'.format(score))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
1

On the last line you need to convert the integer to a string

print('Your score was: ' + str(score))

Alternatively you can use a format string:

print('Your score was: %u' % score)

This is because you cannot concatenate an integer to a string directly, e.g.
"I have " + 7 + " kittens" – in order to do that you have to first convert the integer (in this case 7) to a string. You can do that by using the str() function built into Python.

qff
  • 5,524
  • 3
  • 37
  • 62