''str' object is not callable- tic_tac_toe- Pytnon
I try to make a tic tac toe game and I get:
'str' object is not callable
as error. No idea what to do.
from IPython.display import clear_output
def display_board (board):
clear_output()
print (' ' + board [7] + '|' + board [8] + '|' + board[9])
print (' ' + board [4] + '|' + board [5] + '|' + board[6])
print (' ' + board [1] + '|' + board [2] + '|' + board[3])
def player_input():
marker = " "
while marker != "X" and marker != "O":
marker = input ('Igralec 1: izberi X ali O: ').upper()
if marker == 'X':
return ('X', 'O')
else:
return ('O', 'X')
def place_marker (board, marker, position):
board[position]= marker
def win_check (board, mark):
return ((board[1] == board[2] == board[3] == mark) or #vrsta 1
(board[4] == board[5] == board[6] == mark) or #vrsta 2
(board[5] == board[6] == board[7] == mark) or #vrsta 3
(board[8] == board[5] == board[2] == mark) or #stolpec 2
(board[9] == board[6] == board[3] == mark) or #stolpec 3
(board[7] == board[4] == board[1] == mark) or #stolpec 4
(board[3] == board[5] == board[7] == mark) or #diagonala
(board[1] == board[5] == board[9] == mark)) #diagonala
import random
def choose_first():
flip= random.randint (0,1)
if flip== 0:
return 'Igralec 1'
else:
return 'Player 2'
def space_check (board, position):
board[position] ==' '
def full_board_check (board):
for i in range (1,10):
if space_check (board, i):
return False
return True
def player_choice (board):
position= 0
while position not in [1,2,3,4,5,6,7,8,9] or not space_check (board, position):
position = int(input('Izberi pozicijo: (1-9) '))
return position
def replay():
choice = input ('Želiš še enkrat igrat (Y or N)? ')
return choice ('Y')
print (' Hello, pri tri v vrsto!')
while True:
the_board=[""] *10
player1_marker,player2_marker = player_input()
turn= choose_first()
print= (turn+ ' je prvi igralec!')
play_game= input (' Pripravljeni za igro? y or n ')
if play_game== 'y':
game_on= True
else:
game_on= False
##game time
while game_on:
#igralec 1
if turn== 'Player 1':
display_board(the_board)
position= player_choice(the_board)
place_marker(the_board,player1_marker,position)
#check if they won
if win_check(the_board,player1_marker):
display_board(the_board)
print ('Igralec 1 je zmagovalec!')
game_on = False
else:
if full_board_check(the_board):
display_board(the_board)
print ("Izanačeno!")
game_on= False
else:
turn = 'Igralec 2'
else:
#igralec 2
display_board(the_board)
position= player_choice(the_board)
place_marker(the_board,player2_marker,position)
#check if they won
if win_check(the_board,player2_marker):
display_board(the_board)
print ('Igralec 2 je zmagovalec!')
game_on = False
else:
if full_board_check(the_board):
display_board(the_board)
print ("Izanačeno!")
game_on= False
else:
turn = 'Igralec 1'
if not replay():
break
Errors are:
49 display_board(the_board) \AppData\Local\Temp/ipykernel_10492/3794783463.py in display_board(board) print (' ' + board [7] + '|' + board [8] + '|' + board[9]) TypeError: 'str' object is not callable TypeError 1 print (' Hello pri tri v vrsto!') 2 3 while True:TypeError: 'str' object is not callable
Solution 1:[1]
This line of code:
print= (turn+ ' je prvi igralec!')
Should be written like this:
print(turn+ ' je prvi igralec!')
You can find what goes wrong with the error description hints ;)
Solution 2:[2]
You have the typo = in print(), the fix would be:
print(turn + ' je prvi igralec!')
to prevent such cases install the linter, like flake8 or pylint, they could help you to improve your code. Also check the black formatting tool to improve readability.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marc Albrand |
| Solution 2 |
