[英]Python "TypeError: 'str' object is not callable" - I'm not using "str" in code
[英]Str object is not callable TypeError for Connect four game using Python
我正在通過制作連接四游戲來練習 Python,但我遇到了這個錯誤,我不確定下一步應該采取什么措施來修復它。 文件“main.py”,第 108 行,在 player1() 類型錯誤:'str' object 不可調用
import pandas as pd
data = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0,
0, 0], [0, 0, 0, 0 ,0, 0]]
df = pd.DataFrame(data, columns = ['A', 'B', 'C', 'D', 'E', 'F'])
print(df)
a = df['A']
b = df['B']
c = df['C']
d = df['D']
e = df['E']
f = df['F']
player1 = 'H'
player2 = 'X'
for i in range(len(df)):
a_cell = a[i]
b_cell = b[i]
c_cell = c[i]
d_cell = d[i]
e_cell = e[i]
f_cell = f[i]
# Prevents board piece from changing after changed once
if a[i] == 'X' or a[i] == 'H':
a[i] != player1.inputs or player2.inputs
if b[i] == 'X' or b[i] == 'H':
b[i] != player1.inputs or player2.inputs
if c[i] == 'X' or c[i] == 'H':
c[i] != player1.inputs or player2.inputs
if d[i] == 'X' or d[i] == 'H':
d[i] != player1.inputs or player2.inputs
if e[i] == 'X' or e[i] == 'H':
e[i] != player1.inputs or player2.inputs
if f[i] == 'X' or f[i] == 'H':
f[i] != player1.inputs or player2.inputs
# Places board pieces from input
def player1():
if player1 == "a" and a[i] != "X" and a[i] != "H":
a[i] = "H"
print(df)
elif player1 == "b" and b[i] != 'X' and b[i] != 'H':
b[i] = "H"
print(df)
elif player1 == "c" and c[i] != 'X' and b[i] != 'H':
c[i] = "H"
print(df)
elif player1 == "d" and d[i] != 'X' and b[i] != 'H':
d[i] = "H"
print(df)
elif player1 == "e" and e[i] != 'X' and b[i] != 'H':
e[i] = "H"
print(df)
elif player1 == "f" and f[i] != 'X' and b[i] != 'H':
f[i] = "H"
print(df)
else:
player1 = input("Player1, select a correct board piece: ")
player1()
def player2():
if player2 == "a" and a[i] != "X" and a[i] != "H":
a[i] = "X"
print(df)
elif player2 == "b" and b[i] != 'X' and b[i] != 'H':
b[i] = 'X'
print(df)
elif player2 == "c" and c[i] != 'X' and c[i] != 'H':
c[i] = "X"
print(df)
elif player2 == "d" and d[i] != 'X' and d[i] != 'H':
d[i] = "X"
print(df)
elif player2 == "e" and e[i] != 'X' and e[i] != 'H' :
e[i] = "X"
print(df)
elif player2 == "f" and f[i] != 'X' and f[i] != 'H':
f[i] = "X"
print(df)
else:
player2 = input("Player2, select a correct board piece: ")
player2()
player1 = "H"
player2 = "X"
player1 = input("Player1, select board piece: ")
while True:
if player1 == 'a' or player1 == 'b' or player1 == 'c' or player1 == 'd' or player1 == 'e' or
player1 == 'f':
player1()
player2 = input("Player2, select board piece: ")
elif player2 == 'a' or player2 == 'b' or player2 == 'c' or player2 == 'd' or player2 == 'e' or
player2 == 'f':
player2()
player1 = input("Player1, select board piece: ")
它還說在分配前引用的第 114 行的 scope 中定義了局部變量“player1”,但在第 109 行有一個用於“player2”的變量
您對 function 和字符串使用相同的名稱。 首先,定義名為player1
和player2
的函數。 然后,在第 105 行,當您從input
捕獲文本時,您將一個字符串分配給變量player1
。 因此,當您嘗試在第 110 行調用player1()
時,您會收到錯誤,因為無法調用字符串。
請注意,您還需要為player2
修復此錯誤。 您還在 function 定義本身中重復相同的錯誤,在將這些變量定義為字符串之后遞歸調用函數player1
和player2
。
問題 1: TypeError
您首先將player1
設置為一個字符串,然后定義一個名為player1
的 function ,然后再次將player1
定義為一個字符串(使用input
),然后嘗試調用player1
(在嘗試調用時是一個字符串)。 嘗試為函數想出不同的名稱,例如move_player1
或其他名稱(我將在此答案的第二部分使用此名稱)。
問題 2: UnboundLocalError
至於分配之前引用的變量,您會得到這個,因為當您在player1
之外定義move_player1
時,如果player1
在move_player1
中(重新)定義,則 Python 中的范圍規則會創建一個新的player1
變量(局部變量)。 因此,您不能引用外部player1
並以這種方式重新分配它。 解決此問題的最簡單方法是將player1
聲明為全局。
例如:
player1 = "H"
def move_player1():
global player1
# ... rest of function
盡管您可能還考慮將player1
作為參數傳遞給move_player1
並將其定義(使用input
) move_player1
之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.