0

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.

  • 9
    `input` always returns a string – Alexander Lekontsev Jul 22 '20 at 10:51
  • As @AlexanderLekontsev wrote, your value is always a string. Now, the question is what exactly are you trying to do? Are you just trying to learn about Python, or is there some goal you are trying to accomplish? – zvone Jul 22 '20 at 10:54
  • @zvone hi, yes i am trying to learn about Python. I was trying out few conditional statements to try to calculate the length of string. If the input is string data type, then it should return the number of characters in that string. Otherwise, if the entered data type is int/float, it should return the message in if/elif conditional. – Varun R Jul 22 '20 at 11:02

2 Answers2

0

As your input can be int and float you can use:

def string_length(input_string):
    if input_string.isdigit():
        return "Integer"
    elif input_string.startswith("-") and input_string.count('-') == 1:
        str_ = input_string.replace('-','')
        if input_string.replace('-','').isdigit():
            return "Integer"
        elif input_string.count('.') == 1 and str_.replace('.','').isdigit():
            return "Float"
    elif input_string.count('.') == 1 and input_string.replace('.','').isdigit():
        return "Float"
    else:
        return len(word)
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
  • But then again, `'-1'.isdigit()` returns `False`. There is actually no correct answer, because it is not clear what exactly OP is trying to do. – zvone Jul 22 '20 at 10:55
  • @SaiSreenivas thanks for the blueprint. It returns the datatype output. However, it also returns a None. Enter the string: 12 Integer None. Can you please let me know if there is any error? – Varun R Jul 22 '20 at 11:16
0

Did you mean to have a function which tries these types for you?

def tryTypes(word):
    try:
        print(string_length(int(word)))
        return
    except ValueError as e:
        pass

    try:
        print(string_length(float(word)))
        return
    except ValueError as e:
        pass

    print(string_length(word))

word = input("Enter the string: ")
tryTypes(word)
quamrana
  • 37,849
  • 12
  • 53
  • 71