I was trying a code where upon entering a string, we get the length of the string. However, if we enter an integer or float data type we get the "Wrong data type" message. Please find my code below:
def string_length(word):
if type(word) == int:
return "This is wrong data type"
elif type(word) == float:
return "Wrong data type again!"
else:
return len(word)
word = input("Enter the string: ")
print(string_length(word))
The program is running fine for string input, however, for integers it is returning the number of digits ("characters") instead of the message. Please find below the output terminal window:
PS D:\Python\Practice> python .\stringlength_exercise2.py
Enter the string: Hellooo
7
PS D:\Python\Practice> python .\stringlength_exercise2.py
Enter the string: Hello there
11
PS D:\Python\Practice> python .\stringlength_exercise2.py
Enter the string: 123
3
PS D:\Python\Practice> python .\stringlength_exercise2.py
Enter the string: 12.20
5
Can you please help me identify / rectify the mistake?
Thanks in advance.