变量在函数中 运行 时表示它为 False,但随后又恢复为 True
Variable says it is False when run in the function, but then it reverts back to True
我目前正在为一门课程制作井字游戏。一切进展顺利,但是,我遇到了障碍。当我想检查玩家是否获胜时,我遇到了问题,这是一个函数(称为 "game_over")。当我 运行 游戏时,如果满足 "if" 语句,则显示 "game_on = False"。但是,一旦播放器 while 循环开始,它就会恢复为 True。因此游戏永远不会停止。
game_on = True
player_ready = False
game_finish = False
turn = "Player1"
# THE BOARD
line1 = ["|", "___" , "|", "___", "|","___","|"]
line2 = ["|", "___" , "|", "___", "|","___","|"]
line3 = ["|", "___" , "|", "___", "|","___","|"]
list1 = ''.join(line1)
list2 = ''.join(line2)
list3 = ''.join(line3)
print(list1)
print(list2)
print(list3)
# FUNCTIONS AND THE GAME
Player1_character = "x"
def game_over(player,character):
if line1[1] == (f"_{character}_") and line1[3] == (f"_{character}_") and line1[5] == (f"_{character}_"):
game_on = False
print(f"Game Over, {player} won!game_on = {game_on}")
return
def print_character(numbers,char):
if numbers == 1:
line1[1] = (f"_{char}_")
if numbers == 2:
line1[3] = (f"_{char}_")
if numbers == 3:
line1[5] = (f"_{char}_")
if numbers == 4:
line2[1] = (f"_{char}_")
if numbers == 5:
line2[3] = (f"_{char}_")
if numbers == 6:
line2[5] = (f"_{char}_")
if numbers == 7:
line3[1] = (f"_{char}_")
if numbers == 8:
line3[3] = (f"_{char}_")
if numbers == 9:
line3[5] = (f"_{char}_")
list1 = ''.join(line1)
list2 = ''.join(line2)
list3 = ''.join(line3)
print(list1)
print(list2)
print(list3)
section = 0
# Tutorial Goes Here
while game_on == True:
# Selecting Players
while player_ready == False:
Player1_character = ""
Player1_character = input("Player1 choose your character 'x' or 'o': ")
Player2_character = "|"
if Player1_character == "x":
Player2_character = "o"
elif Player1_character == "o":
Player2_character = "x"
print(f"Player1 = {Player1_character} ||| Player2 = {Player2_character}")
print("LETS BEGIN!!!\n")
player_ready = True
#Starting game:
while turn == "Player1":
section = int(input(f"Player1's Turn: \nSelect where you want to put your '{Player1_character}'(1-9){game_on}: "))
print_character(section, Player1_character)
if game_on == False:
break
turn = "Player2"
while turn == "Player2":
section = int(input(f"Player2's Turn: \nSelect where you want to put your '{Player2_character}'(1-9){game_on}: "))
print_character(section, Player2_character)
game_over("Player2",Player2_character)
if game_on == False:
break
turn = "Player1"
请参阅 the rules for using global variables in functions 上的 Python 编程常见问题解答。
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
我目前正在为一门课程制作井字游戏。一切进展顺利,但是,我遇到了障碍。当我想检查玩家是否获胜时,我遇到了问题,这是一个函数(称为 "game_over")。当我 运行 游戏时,如果满足 "if" 语句,则显示 "game_on = False"。但是,一旦播放器 while 循环开始,它就会恢复为 True。因此游戏永远不会停止。
game_on = True
player_ready = False
game_finish = False
turn = "Player1"
# THE BOARD
line1 = ["|", "___" , "|", "___", "|","___","|"]
line2 = ["|", "___" , "|", "___", "|","___","|"]
line3 = ["|", "___" , "|", "___", "|","___","|"]
list1 = ''.join(line1)
list2 = ''.join(line2)
list3 = ''.join(line3)
print(list1)
print(list2)
print(list3)
# FUNCTIONS AND THE GAME
Player1_character = "x"
def game_over(player,character):
if line1[1] == (f"_{character}_") and line1[3] == (f"_{character}_") and line1[5] == (f"_{character}_"):
game_on = False
print(f"Game Over, {player} won!game_on = {game_on}")
return
def print_character(numbers,char):
if numbers == 1:
line1[1] = (f"_{char}_")
if numbers == 2:
line1[3] = (f"_{char}_")
if numbers == 3:
line1[5] = (f"_{char}_")
if numbers == 4:
line2[1] = (f"_{char}_")
if numbers == 5:
line2[3] = (f"_{char}_")
if numbers == 6:
line2[5] = (f"_{char}_")
if numbers == 7:
line3[1] = (f"_{char}_")
if numbers == 8:
line3[3] = (f"_{char}_")
if numbers == 9:
line3[5] = (f"_{char}_")
list1 = ''.join(line1)
list2 = ''.join(line2)
list3 = ''.join(line3)
print(list1)
print(list2)
print(list3)
section = 0
# Tutorial Goes Here
while game_on == True:
# Selecting Players
while player_ready == False:
Player1_character = ""
Player1_character = input("Player1 choose your character 'x' or 'o': ")
Player2_character = "|"
if Player1_character == "x":
Player2_character = "o"
elif Player1_character == "o":
Player2_character = "x"
print(f"Player1 = {Player1_character} ||| Player2 = {Player2_character}")
print("LETS BEGIN!!!\n")
player_ready = True
#Starting game:
while turn == "Player1":
section = int(input(f"Player1's Turn: \nSelect where you want to put your '{Player1_character}'(1-9){game_on}: "))
print_character(section, Player1_character)
if game_on == False:
break
turn = "Player2"
while turn == "Player2":
section = int(input(f"Player2's Turn: \nSelect where you want to put your '{Player2_character}'(1-9){game_on}: "))
print_character(section, Player2_character)
game_over("Player2",Player2_character)
if game_on == False:
break
turn = "Player1"
请参阅 the rules for using global variables in functions 上的 Python 编程常见问题解答。
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.