我不太清楚如何将输出打印到文件中。如何将每个输出打印到文件中
样品输入
0
2
1
3
5
样品输入
S
SLSLSRS
SLS
SLSLSRSLSLSRSRS
SLSLSRSLSLSRSRSLSLSLSRSRSLSRSRSLSLSLSRSLSLSRSRSRSLSLSRSRSLSRSRS
这里dragon.dat的内容是我的代码:
infile = open("dragon.dat", "w")
def B(n):
if n>22:
return "Enter integer less than 22"
elif n==0:
return "S"
str=B(n-1)
reversestr=str
str +="L"
reversestr=reversestr.replace("L","T")
reversestr=reversestr.replace("R","L").replace("T","R")
reversestr=reversestr[::-1]
return str + reversestr
print(B(0)) # these input will be printed in the python shell
print(B(2))
print(B(1))
print(B(3))
print(B(5))
infile.write(B(0))
infile.write(B(2))
infile.write(B(1))
infile.write(B(3))
infile.write(B(5))
infile.close()
我的输出中的文件:
SSLSLSRSSLSSLSLSRSLSLSRSRSSLSLSRSLSLSRSRSLSLSLSRSRSLSRSRSLSLSLSRSLSLSRSRSRSLSLSRSRSLSRSRS
我怎样才能将它们分成每一行就像样本输出?
使用''\ n'':'infile.write(B(0)+ '\ N')' –