简体   繁体   中英

Str object is not callable TypeError for Connect four game using Python

I'm practicing Python by making a connect four game and I've got stuck at this error and I'm not sure what next steps I should take to fix it. File "main.py", line 108, in player1() TypeError: 'str' object is not callable

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: ")

It also says local variable 'player1' defined in enclosing scope on line 114 referenced before assignment and there's one for 'player2' but on line 109

You are using the same name for a function and a string. First, you define functions called player1 and player2 . Then, on line 105, you assign a string to the variable player1 when you capture the text from input . So, when you then try to call player1() on line 110, you get an error because a string cannot be called.

Note that you need to fix this error for player2 as well. You also repeat the same error in the function definitions themselves, where you are calling the functions player1 and player2 recursively after defining those variables as strings.

Issue 1: TypeError

You set player1 to a string at first, then define a function called player1 , then define player1 as a string again (with input ), then try to call player1 (which at the time of trying to call is a string). Try coming up with different names for the functions, like move_player1 or something (I'll use this name in the second part of this answer).

Issue 2: UnboundLocalError

As for variable referenced before assignment, you're getting this because, while you define player1 outside of move_player1 , scoping rules in Python make a new player1 variable (local variable) if player1 is (re)defined within move_player1 . Thus you can't reference the outer player1 and reassign it in this manner. The simplest way to fix this would be to declare player1 as global.

For example:

player1 = "H"

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

Though you might also consider just passing player1 as an argument to move_player1 and move its definition (using input ) outside of move_player1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM