我定义了一个返回段落字符串的函数,但当我返回它,然后打印它时,它返回:
('| 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)
答案 0 :(得分:1)
你的长字符串实际上不是字符串,而是一系列字符串。如果您将它们传递给print
,它们都会依次打印出来。如果你归还它们,它们会变成一个元组。如果您尝试打印元组,则会得到不同的输出。
您可以通过加入将整批作为一个字符串返回。
return ' '.join([""" | 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]),"""|
--+---+---+---+---+---+---+---+---+---+---+"""])
或者您可以使用连接而不是逗号来构造它。
一旦你将它作为一个字符串而不是一个元组返回,打印它应该可以正常工作。
答案 1 :(得分:0)