I would like to understand why one program of mine is giving error and one not where as I am applying same concept for both of them.
The program that is giving error:
greeting = "test"
age = 24
print( greeting + age)
Which is true and it should give error because of incompatible variable types being concatenated. But this same behavior I was expecting from the below code as well where as it is giving proper result. Why is it so?
print("Please enter your name: ")
myname = input()
print("Your name is " + myname)
print("Please enter your age: ")
myage = input()
print("Your age is: " + myage)
print("Final Outcome is:")
print(myage + " " + myname)
2 Answers 2
By default the input function in Python returns a string type. So when you enter the age, it is not being returned to your program as an int but rather a string. So:
print("Your age is: " + myage)
Actually looks like:
print("Your age is: " + "24")
2 Comments
input() returns a string, even if the user enters a number.
inputcan only return a string because you type characters. It returns'24', not24.raw_input()in Python 2.x andinput()in Python 3.xinput()reads input as strings only.