[英]How to end loop with outside function in Python?
我在 function 外部结束循环时遇到问题。 这是我的蜱虫游戏。 下面是我的代码的一部分,我的 function win(x) 有条件获胜,我希望它完成“while game”循环。 我尝试了很多方法,如果满足 win_x() 的条件,它会打印“X wins”,但循环继续按 range(5) 如何更改 win_x() function 以结束循环?
def win_x():
global game
global win
global board_numbers
if board_numbers[30]=="X" and board_numbers[62]=="X" and board_numbers[94]=="X":
os.system('clear')
print(board_numbers)
print("X wins")
game=True
while game:
os.system('clear')
print(board_numbers)
for i in range(5):
if player_marker=="X" or player_marker=="x":
player_X()
os.system('clear')
win_x()
computer_O()
else:
os.system('clear')
computer_X()
player_O()
我一直在尝试,我想通了。 代码看起来很傻但有效:
def win_x():
global game
global board_numbers
if board_numbers[30]=="X" and board_numbers[62]=="X" and board_numbers[94]=="X":
os.system('clear')
print(board_numbers)
print("X wins")
game=False #placing just game=False on its own didn't help
game=True
while game:
os.system('clear')
print(board_numbers)
for i in range(5):
if player_marker=="X" or player_marker=="x":
player_X()
os.system('clear')
win_x()
if game==False: #it is still weird as game=False was in above function
break #so I am not sure why I had to state it here
computer_O()
else:
os.system('clear')
computer_X()
player_O()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.