#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Why am I getting an error here?


    This works fine:

    Code:
    print "Halt!"
    
    user_input_name = raw_input("Who goes there? ")
    
    print "You may pass this once,", user_input_name + "."
    
    user_input_age = input("Oh, one more thing, how old are you? ")
    
    print "Ahh,", user_input_age
    
    print "That should be fine. Have a nice day."
    ...however if I try and add the following:

    Code:
    print "Halt!"
    
    user_input_name = raw_input("Who goes there? ")
    
    print "You may pass this once,", user_input_name + "."
    
    user_input_age = input("Oh, one more thing, how old are you? ")
    
    print "Ahh,", user_input_age + "."
    #this bit above
    
    print "That should be fine. Have a nice day."
    I get this error:

    Traceback (most recent call last):
    File "(filepath)\example.py", line 9, in <module>
    print "Ahh,", user_input_age + "."
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    You have to use str conversion (built-in) function.
    Code:
    >>> user_input_age = input("Oh, one more thing, how old are you? ")
    Oh, one more thing, how old are you? 120
    >>> 
    >>> print "Ahh,", user_input_age + "."
    Ahh,
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> 
    >>> 
    >>> print "Ahh,", str(user_input_age) + "."
    Ahh, 120.
    >>> 
    >>>
    For more information about str() function, click Here

    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by dariyoosh
    You have to use str conversion (built-in) function.
    Code:
    >>> user_input_age = input("Oh, one more thing, how old are you? ")
    Oh, one more thing, how old are you? 120
    >>> 
    >>> print "Ahh,", user_input_age + "."
    Ahh,
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> 
    >>> 
    >>> print "Ahh,", str(user_input_age) + "."
    Ahh, 120.
    >>> 
    >>>
    For more information about str() function, click Here

    Regards,
    Dariyoosh
    Why did the one above that work whereas the highlighted bit did not?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by ClutchHunter
    Why did the one above that work whereas the highlighted bit did not?
    Because in the first example, you print them separately without concatenating them as a single string.

    For more information, please read print()
    All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end.
    Regards,
    Dariyoosh
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by dariyoosh
    quote
    I thought I did concatenate them, they look identical. I think. I must be missing something obvious
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    That's why I provided the link to the print() function documentation. Separating arguments and concatenating them is not the same thing.

    Code:
    >>> l_list_words = ["hello", "World!"]
    >>> l_word1 = "hello"
    >>> l_word2 = "World!"
    >>> print(l_word1 + " " + l_word2)  # So here you concatenate two strings before printing them
    hello World!
    >>> 
    >>> print l_word1, l_word2 # Here you just print them separately
    hello World!
    >>> 
    >>>
    The result may seem to be the same at the first look but in fact Python does not the same thing.

    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo