0

i'm just starting to learn now and i have been doing some exercises trying to add some inputs to the basics functions i have made .

right now i have this code.

print("This is an app calculate the lenght of a word")

def String_Lenght(word):
    if type(word) == int:
        return "Integers can't be counted"
    elif type(word) == float:
        return "floats can't be counted"
    else:
        return len(word)
word = input("enter the word")
print(String_Lenght(word))

The problem is that i get the len of the word but i'm not getting the messages for int and floats in the case when i introduce one, which would be the error here.

Thanks/

quiell
  • 33
  • 4
  • 5
    `input()` always returns a string. If you input `5` it is just a string of value `'5'`. – sashaaero Nov 13 '18 at 01:58
  • 2
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – James Nov 13 '18 at 02:00
  • You can use a try/except statement in order to see whether the input can get converted into an int / a float. – quant Nov 13 '18 at 02:02
  • @quant You can check my answer below . I think I do porvide a more elegant solution . – Mark White Nov 13 '18 at 03:17

3 Answers3

1

You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.

from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
    try:
        word = literal_eval(word)
    except ValueError:
        pass
    if type(word) == int:
        return "Integers can't be counted"
    elif type(word) == float:
        return "floats can't be counted"
    else:
        return len(word)
word = input("enter the word")
print(String_Lenght(word))
Rish
  • 804
  • 8
  • 15
0

When you read 'word' is always a string in python 3+.

so type(word) is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()

instead of trying to check type(word). I think you should convert string to int / float.

I think converting to float is a best option .

This issue is in python 3. as all data from input is string. you can check here.

$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
  File "t.py", line 13, in <module>
    print(String_Lenght(word))
  File "t.py", line 6, in String_Lenght
    if type(word) == int:
  File "t.py", line 6, in String_Lenght
    if type(word) == int:
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
Seshadri VS
  • 550
  • 1
  • 6
  • 24
0

The reason of this problem is in Python input function always return str type. So in your code type(word) always return True. You should change your code to this.

print("This is an app calculate the lenght of a word")

def String_Lenght(word):
    if word.isdigit():
        return "Integers can't be counted"
    elif word.replace(".", "", 1).isdigit():
        return "floats can't be counted"
    else:
        return len(word)


word = input("enter the word")
print(String_Lenght(word))
Mark White
  • 640
  • 1
  • 5
  • 12