Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str object not callable when converting int variable to using str function

I'm sorry if this was asked already I have been sifting through past questions and cannot seem to find an answer. I am fairly new at python too.

What I am trying to do is write the word and r and c variable to an output file but formatted with a space between the word and variable (the format being the longest word of list - word I am looking at).

I have tried this, yet it comes up with a "str object not callable"

row_variable = str(r)

Here is the traceback when i tried it:

Traceback (most recent call last):
File "C:\CS303E\WordSearch.py", line 106, in <module>
  main ()
File "C:\CS303E\WordSearch.py", line 84, in main
  output.write(list_of_words[i]+spaces+str(row_variable)+str(column_variable))
TypeError: 'str' object is not callable

Here is my code:

# Find longest word in list of words
max_length = 0
for i in list_of_words:
  if len(i) > max_length:
    max_length = len(i)
# Open output file found.txt
output = open('found.txt','w')

# Check for list of words
for i in range(len(list_of_words)):
  flag = False
  for j in range(len(rows)):
    if list_of_words[i] in rows[j]:
      r = j + 1
      c = rows[j].find(list_of_words[i]) + 1
      num_spaces = max_length - len(list_of_words[i])
      spaces = ' ' * num_spaces
      output.write(list_of_words[i])
      flag = True
  for j in range(len(rev_rows)):
    if list_of_words[i] in rev_rows[j]:
      r = j + 1
      c = num_col - rev_rows[j].find(list_of_words[i])
      print(list_of_words[i], r, c)
      flag = True
  for j in range(len(columns)):
    if list_of_words[i] in columns[j]:
      r = j + 1
      c = columns[j].find(list_of_words[i]) + 1
      print(list_of_words[i], c, r) 
      flag = True
  for j in range(len(rev_columns)):
    if list_of_words[i] in rev_columns[j]:
      r = j + 1
      c = num_col - rev_rows[j].find(list_of_words[i])
      print(list_of_words[i], c, r)
      flag = True
  if flag != True:
    print (list_of_words[i],0,0)
like image 979
user3557908 Avatar asked Feb 18 '26 14:02

user3557908


1 Answers

Somewhere in your code (not in the part you have posted here) you have used the name str for a string. str is now that string, and not the built-in string type. You are attempting to call the string as though it were a function or type and Python is calling you out on it.

To avoid this problem, don't name your string str.

like image 190
kindall Avatar answered Feb 21 '26 04:02

kindall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!