What does “Type Error: Can't convert 'int' to str implicitly” mean?

前端 未结 2 1664
陌清茗
陌清茗 2021-01-27 12:21
def display_positive_indices(strlen):
    print()
    print(\' \', end=\'\')
    for i in range(strlen + 1):
        print(i, end=\'\')
        if i != strlen:
                  


        
相关标签:
2条回答
  • 2021-01-27 12:59

    Your function works fine if you call it with an integer, such as:

    In [499]: display_positive_indices(3)
    
     0   1   2   3
    

    But when you call it with a string, you get this error, and the interpreter tells you more information:

    In [500]: display_positive_indices('3')
    
     ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-500-dd39f751056c> in <module>()
    ----> 1 display_positive_indices('3')
    
    <ipython-input-495-ac7e32dd0c50> in display_positive_indices(strlen)
          2     print()
          3     print(' ', end='')
    ----> 4     for i in range(strlen + 1):
          5         print(i, end='')
          6         if i != strlen:
    
    TypeError: Can't convert 'int' object to str implicitly
    

    The problem is that strlen + 1. You're trying to add a str to an int. You get the exact same error with just this:

    In [501]: strlen = '3'
    
    In [502]: strlen + 1
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-502-5a3ed0dba868> in <module>()
    ----> 1 strlen + 1
    
    TypeError: Can't convert 'int' object to str implicitly
    

    In Python 3, trying to add something to a str starts off by trying to implicitly convert that other thing to a str, and, as the error says, you can't do that with an int.

    Meanwhile, for future reference, here's how to debug an error like this:

    To start off with, you know which line in your function has the error. So, keep removing stuff until the error goes away. First:

    def display_positive_indices(strlen):
        for i in range(strlen + 1):
            pass
    

    Same error. So:

    def display_positive_indices(strlen):
        range(strlen + 1)
    

    And again:

    def display_positive_indices(strlen):
        strlen + 1
    

    And:

    def display_positive_indices(strlen):
        strlen
    

    OK, that last one succeeded, so the problem was in strlen + 1. Everything else is irrelevant. So, you've narrowed down what you have to figure out, ask about, and/or understand.

    Finally, if you want us to figure out what's wrong with the main function, and why it's passing a str rather than the int you expected, you'll have to show us that function. (Of the top of my head, my first guess is that you're using input to get a length from the user, and not converting it, possibly because you read the Python 2 docs on input instead of the Python 3 docs. But I'd give that guess a 20% chance of being right at best.)

    0 讨论(0)
  • 2021-01-27 13:03

    Since you require an integer you can coerce it to the type you would like, and if it cannot be converted you will get a TypeError or ValueError:

    ...
    strlen = int(strlen)
    for i in range(strlen + 1):
        ...
    
    0 讨论(0)
提交回复
热议问题