当某个条件为Tru时,用python中的另一个字符串替换列表中的字符串元素

2024-05-16 09:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在网上做一个python课程,在那里我得到了一个任务来做一个tic-tac-toe游戏。我做了所有的事情,但是当占位符被用户选择的块中的O或X替换时,我被列表元素的替换卡住了。我已经尝试了很多方法来永久性地替换列表元素,并且花了几个小时来找到正确运行它的方法,但是它仍然打印相同的tic-tac-toe原始板。你知道吗

    # 2 players should be able to play the game (both sitting at the same computer)
# The board should be printed out every time a player makes a move
# You should be able to accept input of the player position and then place a symbol on the board
#          x1 |  y1  |  z1
#        ---- | ---- | ----
#          x2 |  y2  |  z2
#        ---- | ---- | ----
#          x3 |  y3  |  z3
x1= 'top-left'
y1= 'top-mid'
z1= 'top-right'
x2= 'mid-left'
y2= 'mid-mid'
z2= 'mid-right'
x3= 'btm-left'
y3= 'btm-mid'
z3= 'btm-right'
e = [x1,y1,z1,x2,y2,z2,x3,y3,z3]
board = e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+ \
       '--------|------------|--------'+'\n'+\
        e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+ \
       '--------|------------|--------'+'\n'+\
        e[6]+ ' | '+ e[7]+ ' | '+ e[8]


def instructions():
    print('Instuctions: \n')
    print('Game: tic-tac-toe \n')
    print('Player 1: O and Player 2: X \n\n')
    print(' top-left | top-mid | top-right')
    print(' -------- | ------- | ----------')
    print(' mid-left | mid-mid | mid-right')
    print(' -------- | ------- | ----------')
    print(' btm-left | btm-mid | btm-right')
    print('\n\n')
    print('Choose your input when your turn comes.\n\n')


def player_one(inputvalue):
    global e
    for element in e:
        if element == inputvalue:
            e[e.index(element)]='   O    ' #element that i wanna replace
            break
    query =checkSuccess()
    if (query == True):
        print('Tic-Tac-Toe ! Player1 has won the game! ')
    elif (query == False):
        print(board)    # this still prints the original, old board.


def player_two(input):
    global e
    for element in e:
        if element== input:
            e[e.index(element)] ='   X    '
            break
    query =checkSuccess()
    if (query == True):
        print('Tic-Tac-Toe ! Player2 has won the game! ')
    elif (query == False):
        print(board)
        ticTacToe()

board = e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+ \
        e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+ \
        e[6]+ ' | '+ e[7]+ ' | '+ e[8]

def checkSuccess():
    if(e[0]==e[1]==e[2] or e[3]==e[4]==e[5] or e[6]==e[7]==e[8] or e[0] == e[3] == e[6] or
            e[1]==e[4]==e[7] or e[2]==e[5]==e[8] or e[0]==e[4]==e[8] or e[6]==e[4]==e[2]):
        return True
    else:
        return False

def ticTacToe():
    p1 = input("Player1, Enter your choice \n")
    player_one(p1)
    p2 = input("Player2, Enter your choice \n")
    player_two(p2)

instructions()
print(board)
ticTacToe()

我得到的结果是:

top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player1, Enter your choice 
mid-mid
top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player2, Enter your choice 
btm-right
top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player1, Enter your choice 

o/p仍然是一样的。是否有方法替换元素并打印列表的新值?你知道吗


Tags: ortherightboardinputyourtopdef
2条回答

Board变量只定义一次。它是一个字符串,它是不可变的,当您更改e列表的元素时,它不会神奇地更新。正如Danilo在他的answer中所建议的,您可以创建一个函数,并在每次需要打印电路板时调用它:

from beautifultable import BeautifulTable

def draw_board():
    board = BeautifulTable()
    board.append_row(e[:3])
    board.append_row(e[3:6])
    board.append_row(e[6:])
    print(board)

您可以使用^{}格式化您的电路板。但是,您需要先安装它:

pip install beautifultable

示例:

>>> e[0] = 'X'
>>> draw_board()
+     +    -+     -+
|    X     | top-mid | top-right |
+     +    -+     -+
| mid-left | mid-mid | mid-right |
+     +    -+     -+
| btm-left | btm-mid | btm-right |
+     +    -+     -+

好的,这是一堆代码,请为将来使用检查How to ask a good question的堆栈溢出策略。如果使用listgetset方法发布工作代码,则可以得到更好的答案

首先更改elist not board变量。从名单上看董事会不可靠。您已经使用1个变量创建了另一个变量,现在创建了第二个变量,对1个变量的任何更改都不会影响第二个变量。你知道吗

Soo,只要把一个板变成一个函数,它接受全局参数e,它就工作了。你知道吗

def board():
    global e
    print( e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+\
    '    |      |    '+'\n'+\
    e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+\
    '    |      |    '+'\n'+\
    e[6]+ ' | '+ e[7]+ ' | '+ e[8] )

在那之后,你给你的每个变量加上括号(),因为它现在是一个函数。你知道吗


编辑:

另外,您不需要每次更改变量时都for loop,如果变量有特定的值(即列表中没有重复),您可以使用下一种方法:

if input_string in e: e[ e.index(input_string) ] = "\tX\t" 
else: print("this space is taken, take a hike!!") # or something  

相关问题 更多 >