2015-10-29 132 views
-1

我目前正在编写一个基本的骰子软件密码生成器。打印最终密码组合时遇到问题。我正在打印的字符串是将每个单词打印在新的一行上,而不是一行上的整个字符串。我不太清楚为什么。Python中的打印功能是在新行上打印结果

我的代码如下:

import random 

finalPass="" 

def choosePass(): 
    global finalPass 
    i=0 
    j=0 
    while(i<=5): 
     num="" 
     num=str(random.randint(1,6)) 
     while(j<4): 
      num=num+str(random.randint(1,6)) 
      j+=1 
     f=open('container', 'r') 
     for line in f: 
      if(line[0:5]==num): 
       line=line[6::] 
       line=line.replace(' ','') 
       #Add the new word to the string of old words 
       finalPass+=str(line,) 
     f.close() 
     j=0 
     i+=1 
    #This is printing each word to a new line. I want it to print to the same line 
    print(finalPass) 

choosePass() 
+0

请解决您的帖子 –

+0

感谢你修复它 –

+0

对不起新:)添加代码窗口 – Kiro

回答

1

可以全部替换“\ n”上“”在finalPass串符号印刷

与我之前wnat给你的建议 - 之前问任何问题,尝试在python文档中找到答案并在stackoverflow归档中搜索你的问题。

+0

相当陌生,谢谢!我不认为用''取代\ n。这已经修复了:) – Kiro

+0

仍然没有告诉我为什么它每次都打印到新行。任何人有任何想法 – Kiro

+0

是的,但它是一个初级的有用的信息。 尝试替换所有“\ n”符号@Kiro! – aarexer

0

感谢您的帮助。以为我会分享压轴更有效的代码版本!

import random 
 

 
password="" 
 

 
def choosePass(): 
 
    global password 
 
    for i in range(0,6): 
 
     num="" 
 
     for j in range(0,5): 
 
      num=num+str(random.randint(1,6)) 
 
     f=open('container','r') 
 
     for line in f: 
 
      if(line[0:5]==num): 
 
       line=line[6::].strip() 
 
       password+=line+' ' 
 
     f.close() 
 
    print(password+''+str(random.randint(100,1000))) 
 

 
choosePass()