0
age = input("Enter your age: ")
print("You are " +age+ " years old.")
add_age = input("How much older do you want to be?:")
new_age = int(age + add_age)
print("Congrats! You are now " +new_age+ " years old!")

This is probably one of the easiest and simple fixes, but I've been learning python for about 1 day and I cannot get my head around this error. My code is noted above. I tried with Line 3

new_age = int(age + add_age)

without the int(), but it returns the two texts added together, not the numerical input.

Hopefully, someone can assist me.

Thanks in advance!

Edit: Ouch, rough crowd... New learner and user of the website, thought I'd get a little more leniency in asking a duplicate question. In my defense, I searched for my question before asking and there were topics which came up, but they didn't exactly reach into my exact problem of converting to achieve the value I needed. Maybe they did, but there was a lot of information and as a new learner, everything looks like a jargled mess to me at the moment. Anyways, apologies for the duplicate.

Kyle
  • 13
  • 1
  • 3
  • not an answer to your question, but you should wrap the cast to int in try/except in order for your program not to crash on non-numeric input. – Azrael Nov 02 '18 at 07:55
  • `int(age) + int(add_age)`. Also make the change suggested in the answer to be able to `print` – roganjosh Nov 02 '18 at 07:59
  • Awesome, thanks for the help! – Kyle Nov 02 '18 at 08:23
  • @Kyle No worries about it being marked duplicate - sometimes these are actually helpful as they help point users to the right answers who might land here. The only reason it "closes" the question is to prevent repeating the same answers in addition to the same questions. Especially for a new user, it's understandable to ask a duplicate question... the site's search functionality is notoriously atrocious. PS - Welcome to Stack Overflow! – TylerH Nov 02 '18 at 17:28

1 Answers1

1

In Python, you cannot concatenate a string type with an int type, you need to explicitly convert the int to a string before you can do that. Use this:

print("Congrats! You are now " +str(new_age)+ " years old!")
Wazaki
  • 899
  • 1
  • 8
  • 22
  • Thank you so much for the feedback, however. If my age input is 12 and my add_age input is 22. It will print out "Congrats! You are 1222 years old!" My goal, however, is to print out the added inputs. 12 + 22 = 34 – Kyle Nov 02 '18 at 07:54
  • That's because input returns a string type so when you do `new_age = int(age + add_age)` it concatenates the two strings '12' and '22' into '1222' before converting it to an int. You need to convert each one separately before adding them. This will solve it: `new_age = int(age) + int(add_age)` – Wazaki Nov 02 '18 at 07:56
  • 1
    Aha! Thank you! My issue is solved, however, just out of curiosity, there is no workaround for this? You must convert each individual one? – Kyle Nov 02 '18 at 07:59
  • As far as I know, `input()` only returns String in python, so you will always need to explicitly convert to int or whatever type you are expecting. Another often used method is to wrap the method input() by int like: `int(input("enter an int"))` but I would not advise this since your program will crash when the user enters another type (like a word). It's better to get the int as a String, evaluate the type and print an error message if the input is not the type you except before converting it. – Wazaki Nov 02 '18 at 08:08
  • Okay, great! Thanks for the help. – Kyle Nov 02 '18 at 08:12