对不起,如果问过这个问题,我一直在筛选过去的问题,似乎无法找到答案。我也是python的新手。
我要做的是将单词和r和c变量写入输出文件,但格式化为单词和变量之间的空格(格式是列表中最长的单词 - 我正在查看的单词)。 / p>
我试过这个,但它提出了一个" str对象无法调用"
row_variable = str(r)
这是我尝试时的追溯:
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
这是我的代码:
# 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)
答案 0 :(得分:14)
代码中的某个位置(不在此处发布的部分中),您使用名称str
作为字符串。 str
现在是字符串,而不是内置字符串类型。您试图将字符串称为函数或类型,而Python正在调用它。
要避免此问题,请不要为字符串str
命名。