繁体   English   中英

Python 中的打印函数在新行上打印结果

[英]Print function in Python is printing results on new lines

我目前正在编写一个基本的骰子密码生成器。 打印最终密码组合时遇到问题。 我正在打印的字符串是在新行上打印每个单词,而不是在一行上打印整个字符串。 我不太确定为什么。

我的代码如下:

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()

您可以在打印前替换 finalPass 字符串中“”上的所有“\\n”符号

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

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

 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM