Reputation:
I have the list for my board outside of any functions and I am trying to reset the board when I want to play again. I have tried creating a separate function to reset the board to its original value and return the new board but I can't get it to work. I am trying to just reset the board to blank in the play_again function but it still will not reset the board to blank when I choose play again. I also tried making a separate function to copy the board with the deep copy operator and use at the start of the game although I was not sure how to implement it correctly.
graph = [[' ', '|', ' ', '|', ' '],
['-','+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']]
def draw_graph():
for row in graph:
for col in row:
print(col, end = '')
print()
def play_game():
again = 'yes'
while again == 'yes':
graph = [[' ', '|', ' ', '|', ' '],
['-','+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']]
draw_graph()
won = False
current_player = 'player1'
while not won:
place_spot(current_player)
draw_graph()
won = check_win()
current_player = flip_player(current_player)
again = input("Do you want to play again? (yes/no) ")
print("Thank you for playing.")
Upvotes: 0
Views: 758
Reputation: 155
It's a scope issue.
The graph
defined inside the while block is local to the play_game
function, it is shadowing the package-level graph
variable, not changing it.
You either need to define graph
as global in the play_game
function, or (even better IMO) remove the package-level graph
variable and pass the graph
as an argument to draw_graph
instead.
So this is how I would rewrite your program:
def draw_graph(graph):
for row in graph:
for col in row:
print(col, end = '')
print()
def play_game():
again = 'yes'
while again == 'yes':
graph = [[' ', '|', ' ', '|', ' '],
['-','+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']]
draw_graph(graph)
won = False
current_player = 'player1'
while not won:
place_spot(current_player)
draw_graph(graph)
won = check_win()
current_player = flip_player(current_player)
again = input("Do you want to play again? (yes/no) ")
print("Thank you for playing.")
Upvotes: 1
Reputation: 485
Replacing the draw_graph()
function with this should work:
def draw_graph():
global graph
for row in graph:
for col in row:
print(col, end = '')
print()
You can also replace the code to make the graph variable defined within the function and also passed within it:
def draw_graph(graph):
for row in graph:
for col in row:
print(col, end = '')
print()
def play_game():
graph = [[' ', '|', ' ', '|', ' '],
['-','+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']]
again = 'yes'
while again == 'yes':
graph = [[' ', '|', ' ', '|', ' '],
['-','+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']]
draw_graph()
won = False
current_player = 'player1'
while not won:
place_spot(current_player)
draw_graph(graph)
won = check_win()
current_player = flip_player(current_player)
again = input("Do you want to play again? (yes/no) ")
print("Thank you for playing.")
Upvotes: 0