python 3.x - Why does input() always return a string? -
here code:
age = input("how old you?: ") print (age) print (type(age))
result:
how old you?: 35
35
class 'str' <<--- problem!
but, if use..
age = int(input("how old you?: ")) print (age) print (type(age))
and entered "alex"
how old you?: alex
traceback (most recent call last):
file "/users/kanapatgreenigorn/desktop/test.py", line 1, in age = int(input("how old you?: ")) valueerror: invalid literal int() base 10: 'alex'
if want return string think default if want return number or integer can put int( in front of input. put in 'alex' , reason why came error because 'alex' string. not integer (which int)
Comments
Post a Comment