Python if/else语句不是';即使经过大量检查和重新映射,也不能运行嵌入式代码
这是我的密码:Python if/else语句不是';即使经过大量检查和重新映射,也不能运行嵌入式代码,python,if-statement,Python,If Statement,这是我的密码: 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):
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
(和其他)是全局变量。您可以在函数中读取它们,但不能这样更改它们
如果在If
之前打印spot1
,它将等于'1'
要在main
函数中更新spot1
(或在完整代码中更新engine
),必须在更改其值之前添加global spot1
def main():
choice = input('what is your choice?')# just choose 1a for this example
if choice == '1a':
global spot1
spot1 = currentplayer
@Sayse if choice='1a':spot1=currentplayersorry我已经重温了我的代码和这篇文章,我知道问题出在哪里;在我的示例中,我无法修复ITA,您可以在更改
spot1
的值之前添加global spot1
,它应该可以工作。我只是查看了我的代码,您是对的。对不起,麻烦你了