簡體   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