Home python Throws TypeError: 'str' object is not callable

Throws TypeError: ‘str’ object is not callable

Author

Date

Category

I don’t know how to fix the error in the line:

res + = a ([(a.index (i) + key)% len (a)])

Throws an error:

Traceback (most recent call last):
 File "F: / PYCHARM PROGRAm / 1 / venv / Scripts / 1333213213213.py", line 29, in & lt; module & gt;
  res + = a ([(a.index (i) + key)% len (a)])
TypeError: 'str' object is not callable

P.S A little bit of the essence of the program – This is a Caesar encoder. When I type abc11, it should output bcd12. That is, + 1 character / number.


Answer 1, authority 100%

a = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
n = ''
res = ''
key = 1
enter = input (''). strip ()
abc11
def add (f):
  if f! = '':
    f = int (f)
    f + = key
    f = str (f)
    return f
  else:
    return n
for i, z in enumerate (enter):
  if a.count (z) == 0:
    if z.isnumeric ():
      n + = z
    else:
      n = add (n)
      res + = n
      n = ''
      res + = z
  else:
    n = add (n)
    res + = n
    n = ''
    res + = a [(a.index (z) + key)% len (a)]
n = add (n)
res + = n
n = ''
print (res)
bcd12

Answer 2, authority 100%

Parentheses after the identifier (variable name) indicate a function call. a in this line is not a function, but a string, you are trying to call a string as a function, which is what the error says:

res + = a ([(a.index (i) + key)% len (a)])

The outermost parentheses are not needed. Correctly like this:

res + = a [(a.index (i) + key)% len (a)]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions