-4

I am new learner in python

I have written a code in python..

#!/usr/bin/python
s = raw_input('--> ')
print eval('s+1')

I am getting error like this

  [root@python ~]# python 2.py
  --> 2
  Traceback (most recent call last):
  File "2.py", line 3, in <module>
  print eval('s+1')
  File "<string>", line 1, in <module>
  TypeError: cannot concatenate 'str' and 'int' objects

what could be the reason..

  • 1
    Don't use `eval`: [Eval really is dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). – Matthias Nov 20 '14 at 15:17
  • **Do not use `eval` for data that could possibly come from outside the program in any form. It is a critical security risk that allows the user to run arbitrary code on your computer.** – Karl Knechtel Jul 27 '22 at 04:15

1 Answers1

1

s will be a string, as returned by raw_input. Then you are trying to eval (which you should not do) s + 1. 1 is an integer, and as the error tells you, you cannot add a string to an integer.

If you intend for s to be an int, you can convert it.

s = int(raw_input('--> '))

But again, don't use eval.

s = int(raw_input('--> '))
print s + 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218