-1

我正在尝试在 Python 中重新创建扫雷器,并且当我尝试在 IDLE 中运行代码时,我现在的问题是,当用户输入他们的坐标时,我如何才能将位置从“X”变为“-”,即一块空白的瓷砖。此外,我不介意如何改进我的代码的任何提示。谢谢!

import random

Field = [
LA=["X","X","X","X","X","X","X"],
LB=["X","X","X","X","X","X","X"],
LC=["X","X","X","X","X","X","X"],
LD=["X","X","X","X","X","X","X"],
LE=["X","X","X","X","X","X","X"],
LF=["X","X","X","X","X","X","X"],
LG=["X","X","X","X","X","X","X"]
]

print (Feild)
print("Select row starting from top = 1 and column from left = 0")
numa = random.randint(1,7)
numb = random.randint(0,6)
MINE = "O"

row=9
column = 9
one = "1"
blank = "-"

while row != numa and column != numb:
    print (Field)
    print("Enter a row")
    row = raw_input('Please select a row from rows 1 - 7: ')
    column = raw_input('Please enter a column from columns 1 - 7: ')
    columA = int(column) + 1
    columB = int(column) - 1
    rowA = row + 1
    rowB = row - 1
    if rowA == numa and column  == numb:
        if row == 1:
            del LA[column]
            LA.insert(column, one)
        if row == 2:
            del LB[column]
            LB.insert(column, one)     
        if row == 3:
            del LC[column]
            LC.insert(column, one)   
        if row == 4:
            del LD[column]
            LD.insert(column, one) 
        if row == 5:
            del LE[column]
            LE.insert(column, one)         
        if row == 6:
            del LF[column]
            LF.insert(column, one)  
        if row == 7:
            del LG[column]
            LG.insert(column, one)
    elif rowB == numa and column  == numb:
        if row == 1:
            del LA[column]
            LA.insert(column, one)
        if row == 2:
            del LB[column]
            LB.insert(column, one)     
        if row == 3:
            del LC[column]
            LC.insert(column, one)   
        if row == 4:
            del LD[column]
            LD.insert(column, one) 
        if row == 5:
            del LE[column]
            LE.insert(column, one)         
        if row == 6:
            del LF[column]
            LF.insert(column, one)  
        if row == 7:
            del LG[column]
            LG.insert(column, one)       
    elif row == numa and columA  == numb: 
        if row == 1:
            del LA[column]
            LA.insert(column, one)
        if row == 2:
            del LB[column]
            LB.insert(column, one)     
        if row == 3:
            del LC[column]
            LC.insert(column, one)   
        if row == 4:
            del LD[column]
            LD.insert(column, one) 
        if row == 5:
            del LE[column]
            LE.insert(column, one)         
        if row == 6:
            del LF[column]
            LF.insert(column, one)  
        if row == 7:
            del LG[column]
            LG.insert(column, one)
    elif row  == numa and columB == numb:
        if row == 1:
            del LA[column]
            LA.insert(column, one)
        if row == 2:
            del LB[column]
            LB.insert(column, one)     
        if row == 3:
            del LC[column]
            LC.insert(column, one)   
        if row == 4:
            del LD[column]
            LD.insert(column, one) 
        if row == 5:
            del LE[column]
            LE.insert(column, one)         
        if row == 6:
            del LF[column]
            LF.insert(column, one)  
        if row == 7:
            del LG[column]
            LG.insert(column, one)        
    else:
        if row == 1:
            del LA[column]
            LA.insert(column, blank)
        if row == 2:
            del LB[column]
            LB.insert(column, blank)     
        if row == 3:
            del LC[column]
            LC.insert(column, blank)   
        if row == 4:
            del LD[column]
            LD.insert(column, blank) 
        if row == 5:
            del LE[column]
            LE.insert(column, blank)         
        if row == 6:
            del LF[column]
            LF.insert(column, blank)  
        if row == 7:
            del LG[column]
            LG.insert(column, blank) 



if row == 1:
    del LA[column]
    LA.insert(column, MINE)
if row == 2:
    del LB[column]
    LB.insert(column, MINE)     
if row == 3:
    del LC[column]
    LC.insert(column, MINE)   
if row == 4:
    del LD[column]
    LD.insert(column, MINE) 
if row == 5:
    del LE[column]
    LE.insert(column, MINE)         
if row == 6:
    del LF[column]
    LF.insert(column, MINE)  
if row == 7:
    del LG[column]
    LG.insert(column, MINE)
print (Field)
print("Game over")
4

1 回答 1

1

你得到一个错误的说法TypeError: cannot concatinate 'str' and 'int' objects吗?

您需要修复这些行

rowA = row + 1
rowB = row - 1

row你忘了投到int.

实际上,查看您的代码,我认为您应该int在输入时进行转换。

row = int(raw_input())
于 2013-04-22T02:24:26.367 回答