We’re going to discuss converting a string into an integer(int) now in the last article we ask the user for their name we use the input function to allow the user to enter their name so in our example the user typed in Jack we later printed jack using the variable name
The Output will be: What is You Name?..
User Enter “Jack”:
Final Output Hi Jack
Now this time what we’re going to do is we’re going to ask the user for their age so I’m going to start off by calling this variable age equals and than we use the input function once again because I would like the user to enter their age input open my speech marks and my brackets and I’m going to ask them the question how old are you question mark and space.
Now what I would like to do with the age is do some maths with it so I’m going to say and I’m going to print this off I’m gonna just do age plus 5 now there’s going to be a problem with this and the problem is because I’m trying to actually add an integer and a string together and there’s a clash
The reason for the error whenever we use the input function it always returns a string even if the user types in two numbers or any number or an integer it will return it as a string and we can’t do maths with a string which is a problem because we try to add five which is an integer to the age which has been returned to us as a string
Solution: Convert Int to String
So how we resolve that is we have to now convert age from a string into an integer so the best way to do that is to type in age equals and then use the int function which is short for integer open my brackets and type in age again so what it’s doing is it’s returning an integer back into age so now if I print this we get this result: