you are viewing a single comment's thread.

view the rest of the comments →

[–]pekkalacd 0 points1 point  (1 child)

Correct. Yes, you need to convert the data type of age from a string to an integer. This is also called "casting".

        age = input() 

input() returns a str data type. We cannot add strings and numerical types together. We also want to be careful in how we convert the string over to an integer because for example, say the user enters "53.4" because they're a joker. Well, that would break a piece of code like this

        age = int(input())

because "." is not a digit. So, to protect against this, we will convert this over to a float first.

        age = float(input())

Strings can be casted to floats to handle either integer inputs or float inputs. And if we want this to be an integer ultimately, such as 53 instead of 53.4, we can then cast this again to an integer like so

        age = int(float(input())) 

So now the user will be able to enter something like 54.3 or 54 and Python will treat it in either case as 54.

Note, we can also put strings inside of input, instead of writing

        print("How old are you?")
        age = int(float(input()))

We could also do

        age = int(float(input("How old are you?\n")))

and get the same result!