0

So i decided to make a small string of code that converts your age into lion years (Basically multiplicating your age by 5). The problem i have is i cant get it to take the age as an integer and input, that or i cant make it print the "lionage" integer below.

Code and error sign below:

name = input ("please type your name in: ")
age = int(input ("please type your age in: "))
age = int(age)
five = int(str(5))
lionage = int(age*five)

print ("Hello " + name + "! Your age is " + age + " but in lion years it's " + str(lionage) + " years")

Error: can't convert 'int' object to str implicitly

I may be wrong on what I got wrong in the code.

(By the way, please prioritize giving me an answer of why its going wrong and how I can fix it simplifying the code to make it smaller. (i want to learn that myself, thanks :) )

3 Answers3

0

Around all variables add str().

For example:

a = 21
print("Your age is " + str(a))

This would output "Hello World" (without speech marks).

0

Python can print number, string, ... pretty much anything as long as types are not mixed up. In your case, age is a number and the rest of the line you want to print is a string. You need to convert your number to a string by adding str(age) instead of just age.

Gwendal Grelier
  • 526
  • 1
  • 7
  • 21
0

Age is a integer and you trying to concat with an string. In python, you cannot do that directly.

print ("Your age is " + age)) # <- wrong answer

you must convert age to string before concating

print ("Your age is " + str(age)) # <- Correct answer
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
Freddy
  • 779
  • 1
  • 6
  • 20