【问题标题】:Str object is not callable TypeError for Connect four game using PythonStr object is not callable TypeError for Connect 4 game using Python
【发布时间】: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


【解决方案1】:

问题 1: TypeError

您首先将player1 设置为字符串,然后定义一个名为player1 的函数,然后再次将player1 定义为字符串(使用input),然后尝试调用player1(在尝试调用的时间是一个字符串)。尝试为函数想出不同的名称,例如 move_player1 或其他名称(我将在此答案的第二部分使用此名称)。

问题 2: UnboundLocalError

至于在赋值之前引用的变量,你得到这个是因为,当你在 move_player1 之外定义 player1 时,如果 player1 是 (重新)在move_player1 中定义。因此,您不能引用外部 player1 并以这种方式重新分配它。解决此问题的最简单方法是将player1 声明为全局。

例如:

player1 = "H"

def move_player1():
    global player1
    # ... rest of function

尽管您也可以考虑将player1 作为参数传递给move_player1 并将其定义(使用input)移到move_player1 之外。

【讨论】:

    【解决方案2】:

    您对函数和字符串使用相同的名称。首先,定义名为player1player2 的函数。然后,在第 105 行,当您从 input 捕获文本时,将字符串分配给变量 player1。因此,当您尝试在第 110 行调用 player1() 时,您会收到错误,因为无法调用字符串。

    请注意,您还需要为player2 修复此错误。您还会在函数定义本身中重复相同的错误,在将这些变量定义为字符串之后递归调用函数 player1player2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-21
      • 2014-10-19
      • 2023-03-18
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      相关资源
      最近更新 更多