如何解决即使经过大量检查和重新映射,if / else语句也不会运行嵌入式代码
这是我的代码:
x_or_o = ['x','o']
player_alive = 'yes'
def game_over():
player_alive = 'no'
print('Player '+currentplayer+' wins!')
player_alive = input('Play again? ')
def check():
for n in range(2):
if str(x_or_o[n]) == currentplayer:
if str(spot1) == currentplayer:
game_over()
def main():
choice = input('what is your choice?')# just choose 1a for this example
if choice == '1a':
spot1 = currentplayer
currentplayer = 'x'
check()
由于某种原因,第二个if语句没有运行,即使看起来字符串设置正确并且我尝试更改所有参数,而我得到的第一个if语句正常工作;我尝试更改条件,将spot1更改为字符串并返回,甚至疏远了其他python文件中的代码块,但似乎没有任何效果 完整代码供参考:
import random
import os
spot1 = '1'
spot2 = '2'
spot3 = '3'
spot4 = '4'
spot5 = '5'
spot6 = '6'
spot7 = '7'
spot8 = '8'
spot9 = '9'
currentplayer = 'x'
choice = ''
x_or_o = ['x','o']
player_alive = 'yes'
def game_over():
player_alive = 'no'
print('Player '+currentplayer+' wins!')
player_alive = input('Play again? ')
def check():
for n in range(2):
if str(x_or_o[n]) == currentplayer:
if str(spot1) == currentplayer:# and spot4 == currentplayer and spot7 == currentplayer:#colums,left to right
game_over()
elif spot2 == currentplayer and spot5 == currentplayer and spot8 == currentplayer:
game_over()
elif spot3 == currentplayer and spot6 == currentplayer and spot9 == currentplayer:
game_over()
elif spot1 == currentplayer and spot2 == currentplayer and spot3 == currentplayer:#rows,top to bottom
game_over()
elif spot4 == currentplayer and spot5 == currentplayer and spot6 == currentplayer:
game_over()
elif spot7 == currentplayer and spot8 == currentplayer and spot9 == currentplayer:
game_over()
elif spot1 == currentplayer and spot5 == currentplayer and spot9 == currentplayer:
game_over()
elif spot3 == currentplayer and spot5 == currentplayer and spot7 == currentplayer:
game_over()
def engine():
spot1 = '*'
spot2 = '*'
spot3 = '*'
spot4 = '*'
spot5 = '*'
spot6 = '*'
spot7 = '*'
spot8 = '*'
spot9 = '*'
currentplayer = 'x'
while player_alive == 'yes':
print(' a b c \n')
print('1 ['+spot1+'] ['+spot2+'] ['+spot3+']\n')
print('2 ['+spot4+'] ['+spot5+'] ['+spot6+']\n')
print('3 ['+spot7+'] ['+spot8+'] ['+spot9+']\n')
choice = input('Where would you like to place your piece,'+currentplayer+'?\n')
if choice == '1a':
spot1 = currentplayer
elif choice == '1b':
spot2 = currentplayer
elif choice == '1c':
spot3 = currentplayer
elif choice == '2a':
spot4 = currentplayer
elif choice == '2b':
spot5 = currentplayer
elif choice == '2c':
spot6 = currentplayer
elif choice == '3a':
spot7 = currentplayer
elif choice == '3b':
spot8 = currentplayer
elif choice == '3c':
spot9 = currentplayer
if currentplayer == 'x':
currentplayer = 'o'
else:
currentplayer = 'x'
check()
while True:
engine()
解决方法
您的spot1
(和其他)是全局变量。您可以在函数中阅读它们,但不能像这样更改它们。
如果在spot1
之前打印if
,将等于'1'
要在spot1
函数(或完整代码中的main
)内更新engine
,必须在更改值之前添加global spot1
def main():
choice = input('what is your choice?')# just choose 1a for this example
if choice == '1a':
global spot1
spot1 = currentplayer
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。