1

我有这个代码;

   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

ETC...

我努力了;

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

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

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

如果可以,请提供帮助,并且解释也将是首选。

谢谢你的时间。

4

3 回答 3

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)

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

于 2013-04-26T08:03:29.563 回答
1

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

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

在开始for循环之前。

于 2013-04-26T08:04:38.997 回答
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
于 2013-04-26T08:10:30.063 回答