Reputation: 3
I have an extremely basic tic tac toe game in python. For some reason, the game does not quit when the user enters "e". Why is this?
import os
import sys
os.system("cls")
one = "1"
two = "2"
three = "3"
four = "4"
five = "5"
six = "6"
seven = "7"
eight = "8"
nine = "9"
selection = ""
def instructions():
print("""
Welcome to Tic-Tac-Toe!
To Play, Enter a number between 1-9 to select that space
""")
def board():
print("\t{} | {} | {}\n\t---------\n\t{} | {} | {}\n\t---------\n\t{} | {} | {}".format(one,two,three,four,five,six,seven,eight,nine))
selection = input("\nSelect a number:\n")
os.system("cls")
instructions()
while selection != "e":
board()
Upvotes: 0
Views: 321
Reputation: 9203
For a simple reason that the selection variable in the board and the one checked outside is not the same.
The quick fix would be to add
global selection
at the start of the function board. You should consider reading about scopes of variables in Python.
The final code will be
def board():
global selection
print("\t{} | {} | {}\n\t---------\n\t{} | {} | {}\n\t---------\n\t{} | {} | {}".format(one,two,three,four,five,six,seven,eight,nine))
selection = input("\nSelect a number:\n")
os.system("cls")
Upvotes: 2