Jump to content

Question about an error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
live504

live504

    Newbie

  • Members
  • PipPip
  • 12 posts
>>> s = input("Place value here: ")
Place value here: 5
>>> s+8
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
s+8
TypeError: Can't convert 'int' object to str implicitly
>>>


How come this gives me an error and doesn't give me 13? I'm really sorry if this is a stupid question, I'm new to programing so I don't know much.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I'm not a Python programmer but I believe that input() accepts a string, the string "5" cannot have 8 added to it. Try using the raw_input() function in place of input():

 s = raw_input("Place value here: ")
This should allow you to accept an integer (or what you wish to input) into a variable.

For the future you could also (hopefully) use the int() function to change the typecast:

>>> s = input("Place  value here: ")
Place value here: 5
>>> int(s) + 8

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
CatatonicMan

CatatonicMan

    Newbie

  • Members
  • PipPip
  • 13 posts
I'm assuming you are using Python 3.x, since the example seems to work in 2.x.

In 3.x, the 'raw_input' function is now the 'input' function, and the old 'input' function is now effectively 'eval(input())'. Since the new 'input' always returns a string, and since Python prefers to do things explicitly rather than implicitly, you will need to convert the string into a number before you can use it as one.