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

2024-04-26 14:03:45 发布

您现在位置: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()

Tags: theto字符串密码newlinerandomnum
2条回答

谢谢你的帮助。我想我会分享最后一个更有效的版本的代码!你知道吗

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

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

我给你一些建议——在问任何问题之前,试着在python文档中找到答案,并在stackoverflow存档中搜索你的问题。你知道吗

相关问题 更多 >