Problem with input and return in python app for count words












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/










share|improve this question


















  • 5





    input() always returns a string. If you input 5 it is just a string of value '5'.

    – sashaaero
    Nov 13 '18 at 1:58






  • 2





    Possible duplicate of How can I read inputs as integers?

    – James
    Nov 13 '18 at 2: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 2:02











  • @quant You can check my answer below . I think I do porvide a more elegant solution .

    – Mark White
    Nov 13 '18 at 3:17
















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/










share|improve this question


















  • 5





    input() always returns a string. If you input 5 it is just a string of value '5'.

    – sashaaero
    Nov 13 '18 at 1:58






  • 2





    Possible duplicate of How can I read inputs as integers?

    – James
    Nov 13 '18 at 2: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 2:02











  • @quant You can check my answer below . I think I do porvide a more elegant solution .

    – Mark White
    Nov 13 '18 at 3:17














0












0








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/










share|improve this question














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/







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 1:53









Carlos LeotaudCarlos Leotaud

1




1








  • 5





    input() always returns a string. If you input 5 it is just a string of value '5'.

    – sashaaero
    Nov 13 '18 at 1:58






  • 2





    Possible duplicate of How can I read inputs as integers?

    – James
    Nov 13 '18 at 2: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 2:02











  • @quant You can check my answer below . I think I do porvide a more elegant solution .

    – Mark White
    Nov 13 '18 at 3:17














  • 5





    input() always returns a string. If you input 5 it is just a string of value '5'.

    – sashaaero
    Nov 13 '18 at 1:58






  • 2





    Possible duplicate of How can I read inputs as integers?

    – James
    Nov 13 '18 at 2: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 2:02











  • @quant You can check my answer below . I think I do porvide a more elegant solution .

    – Mark White
    Nov 13 '18 at 3:17








5




5





input() always returns a string. If you input 5 it is just a string of value '5'.

– sashaaero
Nov 13 '18 at 1:58





input() always returns a string. If you input 5 it is just a string of value '5'.

– sashaaero
Nov 13 '18 at 1:58




2




2





Possible duplicate of How can I read inputs as integers?

– James
Nov 13 '18 at 2:00





Possible duplicate of How can I read inputs as integers?

– James
Nov 13 '18 at 2: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 2:02





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 2:02













@quant You can check my answer below . I think I do porvide a more elegant solution .

– Mark White
Nov 13 '18 at 3:17





@quant You can check my answer below . I think I do porvide a more elegant solution .

– Mark White
Nov 13 '18 at 3:17












3 Answers
3






active

oldest

votes


















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))





share|improve this answer































    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





    share|improve this answer































      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))





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272658%2fproblem-with-input-and-return-in-python-app-for-count-words%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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))





        share|improve this answer




























          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))





          share|improve this answer


























            1












            1








            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))





            share|improve this answer













            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))






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 3:15









            Rishabh MishraRishabh Mishra

            368310




            368310

























                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





                share|improve this answer




























                  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





                  share|improve this answer


























                    0












                    0








                    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





                    share|improve this answer













                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 2:09









                    Seshadri VSSeshadri VS

                    19314




                    19314























                        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))





                        share|improve this answer




























                          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))





                          share|improve this answer


























                            0












                            0








                            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))





                            share|improve this answer













                            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))






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 3:14









                            Mark WhiteMark White

                            422310




                            422310






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272658%2fproblem-with-input-and-return-in-python-app-for-count-words%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation