【发布时间】:2020-08-09 07:59:54
【问题描述】:
我正在通过制作四人连接游戏来练习 Python,但我遇到了这个错误,我不确定接下来应该采取什么步骤来修复它。 文件“main.py”,第 108 行,在 播放器1() TypeError: 'str' 对象不可调用
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 行的封闭范围中定义了局部变量“player1”,并且有一个用于“player2”但在第 109 行
【问题讨论】:
-
你应该有数据帧的字典,这样你就可以简单地访问
dfs[player1][i]而不是使用巨大的if语句来选择正确的数据帧。 -
感谢您的反馈,我将不得不考虑为数据帧提供一个字典
-
发布的答案是否有帮助?如果没有,如何改进?
标签: python pandas dataframe typeerror function-call