-6

So when I'm trying to run this code. As you can see it asks for a number as an input.

print('How old is Vini?')
number= 17
guess= int(input('enter a number'))
if guess== number:
    print('Bravo Vini is 17')
elif guess < number:
    print('Vini is older than '+guess)
elif guess > number:
    print('Vini younger than '+guess)

Now if the number is equal to 17 it executes but if the number is higher or lower it gives me this error:

When the number is higher:

Traceback (most recent call last):
  File "C:\Users\ervin\Desktop\loja.py", line 9, in <module>
    print('Vini younger than '+guess)
TypeError: must be str, not int

When the number is lower:

Traceback (most recent call last):
  File "C:\Users\ervin\Desktop\loja.py", line 7, in <module>
    print('Vini is older than '+guess)
TypeError: must be str, not int
jordanm
  • 33,009
  • 7
  • 61
  • 76
Erson Cokaj
  • 36
  • 1
  • 7

2 Answers2

0

You need to convert guess to a string for the print statements to work, try writing + str(guess) instead of + guess.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
0

In statement print('Vini is older than '+guess) you are trying to concatenate a string('Vini is older than ') with an integer(17) which is wrong hence the error that you are getting.

Use

 print('Vini is older than '+str(guess))

str(guess) converts the integer to a string. Now you are concatenating two strings which is valid.

Shashank Singh
  • 647
  • 1
  • 5
  • 22