Python 使用str函数将int变量转换为时,str对象不可调用
很抱歉,如果这个问题已经被问到了,我一直在筛选过去的问题,似乎找不到答案。我对python也相当陌生 我试图做的是将word、r和c变量写入输出文件,但在word和变量之间使用空格进行格式化(格式是我所看到的列表单词中最长的单词) 我已经试过了,但它给出了一个“str对象不可调用” 这是我尝试时的回溯:Python 使用str函数将int变量转换为时,str对象不可调用,python,string,python-3.x,Python,String,Python 3.x,很抱歉,如果这个问题已经被问到了,我一直在筛选过去的问题,似乎找不到答案。我对python也相当陌生 我试图做的是将word、r和c变量写入输出文件,但在word和变量之间使用空格进行格式化(格式是我所看到的列表单词中最长的单词) 我已经试过了,但它给出了一个“str对象不可调用” 这是我尝试时的回溯: Traceback (most recent call last): File "C:\CS303E\WordSearch.py", line 106, in <module>
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
在代码中的某个地方(不是您在此处发布的部分),您使用了名称str
作为字符串str
现在是该字符串,而不是内置的字符串类型。您试图调用字符串,就好像它是一个函数或类型,Python正在调用它
为避免此问题,请不要将字符串命名为
str
,语言可能是?(很少有人对任何只标记了string
的东西感兴趣。此外,除非标记正确,否则语法高亮显示将不起作用。)在我看来很像python。很抱歉,我以为这是一个全python的网站。感谢您指出。搜索WordSearch.py
文件以查找替换str=..
的位置,除非有人恶意地将内置.str
分配到某个位置。我确实这样做了,我认为这不会造成干扰。谢谢。我确实这么做了。我不认为它会干扰,但我想是的。谢谢。如果你必须称它为非描述性的东西,如str
,你可以使用str
或\u-str
。我个人喜欢称这些变量为text
,而不是str
。
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)