附加到语句输出的前面

时间:2013-04-26 07:57:04

标签: python

我有这段代码;

   offset = -0
    print ("In Command 3 - Brute force")
    string = input("Please enter a string to Brute Force:")
    while offset > -26:
        offset = offset - 1
        print("")
        for letter in string:
            letter = (ord(letter))
            letter = letter + offset
            if letter > 126:
                letter - 95
            elif letter < 32:
                letter + 32
            output = (chr(letter))
            print(output,end='')
            choice = 0

输出取决于字符串类似的东西;

rc`rcr
qb_qbq
pa^pap
o`]o`o
n_\n_n
m^[m^m
l]Zl]l
k\Yk\k
j[Xj[j
iZWiZi
hYVhYh
gXUgXg
fWTfWf
eVSeVe
dURdUd
cTQcTc
bSPbSb
aROaRa
`QN`Q`
_PM_P_
^OL^O^
]NK]N]
\MJ\M\
[LI[L[
ZKHZKZ
YJGYJY

现在,我需要输出前的一些文字,例如;

Decryption string rc`rcr
Decryption string qb_qbq

等...

我试过了;

print("Decryption",output,end='')

print("Decryption"+output,end='')

然而,这给了我每个字母前面的文字。

如果可以,请提供帮助,也可以优先解释。

感谢您的时间。

3 个答案:

答案 0 :(得分:2)

你想做这样的事情:

offset = -0
print ("In Command 3 - Brute force")
string = input("Please enter a string to Brute Force:")
while offset > -26:
    offset = offset - 1
    word = ""
    for letter in string:
        letter = (ord(letter))
        letter = letter + offset
        if letter > 126:
            letter - 95
        elif letter < 32:
            letter + 32
        output = (chr(letter))
        word = word + output            
        choice = 0
    print("Decryption: "+word)

您尝试的问题是,它会为每个字符打印“Decrypting:”消息,而不是每个字,因此您需要在打印之前构建该字。

答案 1 :(得分:1)

您正在逐字母打印输出,因此添加print("Decryption"+output,end='')只会在每个打印输出中添加“解密”部分。我建议做一个:

print("Decryption" + string, end=' ')

在开始for循环之前。

答案 2 :(得分:1)

您需要构建输出字符串,然后在for循环后打印

   offset = -0
    print ("In Command 3 - Brute force")
    string = input("Please enter a string to Brute Force:")
    while offset > -26:
        offset = offset - 1
        output_final = None
        for letter in string:
            letter = (ord(letter))
            letter = letter + offset
            if letter > 126:
                letter - 95
            elif letter < 32:
                letter + 32
            output_final += (chr(letter))
            choice = 0
        print 'Description:', output_final