【发布时间】:2019-05-18 16:59:58
【问题描述】:
我正在定义一个函数来返回一个段落字符串,但是当我返回它,然后打印它时,它会返回这个:
(' | a | b | c | d | e | f | g | h | i | j |\n --+---+---+---+---+--- +---+---+---+---+---+\n 10|', ' | | | ♥ | | | ♥ | | | ', '|\n --+-- -+---+---+---+---+---+---+---+---+---+\n 9 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+- --+\n 8 |', ' | | | | | | | | ', '|\n --+---+---+---+---+---+-- -+---+---+---+---+\n 7 |', '♥ | | | | | | | | | ♥', '|\n --+---+- --+---+---+---+---+---+---+---+---+\n 6 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+ \n 5 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+- --+---+---+---+\n 4 |', '♠ | | | | | | | | | ♠', '|\n --+---+---+ ---+---+---+---+---+---+---+---+\n 3 |', ' | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+\n 2 |', ' | | | | | | | | | ', '|\n --+--- +---+---+---+---+---+---+---+---+---+\n 1 |', ' | | | ♠ | | | ♠ | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+' )
但是如果我在函数内部打印它,而不是返回它。这就是结果。
| a | b | c | d | e | f | g | h | i | j |
--+---+---+---+---+---+---+---+---+---+---+
10| | | | ♥ | | | ♥ | | | |
--+---+---+---+---+---+---+---+---+---+---+
9 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
8 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
7 | ♥ | | | | | | | | | ♥ |
--+---+---+---+---+---+---+---+---+---+---+
6 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
5 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
4 | ♠ | | | | | | | | | ♠ |
--+---+---+---+---+---+---+---+---+---+---+
3 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
2 | | | | | | | | | | |
--+---+---+---+---+---+---+---+---+---+---+
1 | | | | ♠ | | | ♠ | | | |
--+---+---+---+---+---+---+---+---+---+---+
这是代码,希望对你有帮助
tableron=[]
for i in range(10):
tableron.append([" "]*10)
columnas=["a","b","c","d","e","f","g","h","i","j"]
def cargar_tablero():
tablero=[]
archivo=open("tablero.txt","r")
for line in archivo:
line=line.strip()
tablero.append(line.split(","))
return tablero
def nuevo_tablero():
tablero=[["a4","d1","g1","j4"],["a7","d10","g10","j7"],[],["1"]]
return tablero
def tablero_to_string(tablero):
lista=tablero[0]
for m in lista:
tableron[int(m[1::])-1].pop(columnas.index(m[0]))
tableron[int(m[1::])-1].insert(columnas.index(m[0]),"♠")
lista=tablero[1]
for m in lista:
tableron[int(m[1::])-1].pop(columnas.index(m[0]))
tableron[int(m[1::])-1].insert(columnas.index(m[0]),"♥")
if tablero[3]!= None:
lista=tablero[2]
for m in lista:
tableron[int(m[1::])-1].pop(columnas.index(m[0]))
tableron[int(m[1::])-1].insert(columnas.index(m[0]),"*")
print (""" | a | b | c | d | e | f | g | h | i | j |
--+---+---+---+---+---+---+---+---+---+---+
10|"""," | ".join(tableron[9]),"""|
--+---+---+---+---+---+---+---+---+---+---+
9 |"""," | ".join(tableron[8]),"""|
--+---+---+---+---+---+---+---+---+---+---+
8 |"""," | ".join(tableron[7]),"""|
--+---+---+---+---+---+---+---+---+---+---+
7 |"""," | ".join(tableron[6]),"""|
--+---+---+---+---+---+---+---+---+---+---+
6 |"""," | ".join(tableron[5]),"""|
--+---+---+---+---+---+---+---+---+---+---+
5 |"""," | ".join(tableron[4]),"""|
--+---+---+---+---+---+---+---+---+---+---+
4 |"""," | ".join(tableron[3]),"""|
--+---+---+---+---+---+---+---+---+---+---+
3 |"""," | ".join(tableron[2]),"""|
--+---+---+---+---+---+---+---+---+---+---+
2 |"""," | ".join(tableron[1]),"""|
--+---+---+---+---+---+---+---+---+---+---+
1 |"""," | ".join(tableron[0]),"""|
--+---+---+---+---+---+---+---+---+---+---+""")
tablero=nuevo_tablero()
tablero_to_string(tablero)
【问题讨论】:
-
你的问题是什么,你能展示你的代码吗?
-
对不起,我刚刚更新了它
-
问题是如何让第一个输出看起来像第二个,但返回一个字符串?
标签: python string algorithm paragraph