当某个条件为True时,将list中的字符串元素替换为python中的另一个字符串

时间:2018-04-20 17:32:39

标签: python python-3.x

所以我正在网上做一个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仍然相同。有没有办法替换元素并打印列表的新值?

2 个答案:

答案 0 :(得分:1)

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)

您可以使用BeautifulTable格式化您的主板。但是,您需要先安装它:

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 |
+----------+---------+-----------+

答案 1 :(得分:-1)

好的这是一堆代码,请将来查看堆栈溢出策略的How to ask a good question。如果您使用列表getset方法

发布工作代码,则可以获得更好的答案

首先,您要更改e列表而非更改电路板变量。董事会不可靠于电子清单。您已使用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 ,如果变量具有特定值(在列表中也没有重复),您可以使用下一个方法:

putIp(ip, data): any {
const url = `${this.IpUrl}/${ip}`;
return this.authHttp.put(url, data, {headers: this.headers});}