簡體   English   中英

IndexError:字符串索引超出范圍 - Python

[英]IndexError: string index out of range - Python

我收到這個錯誤。

mac_decrypt+=decrypt_datei[i]
IndexError: string index out of range

我嘗試了一切,但沒有成功。 有人可以幫助我嗎,我在哪里做錯了!

這是代碼:

lauf = len(decrypt_datei) - 1
nachricht_decrypt = ''
nachricht_length = ord(decrypt_datei[lauf])
nachricht_length = len(decrypt_datei) - (nachricht_length + 1)
lauf -= 1
while nachricht_length <= lauf:
    nachricht_decrypt += decrypt_datei[nachricht_length]
    nachricht_length += 1
print('entschluesselung fertig!')
print('mac Ueberpruefung:')

while s == False:
    # Mac UeberprUefen/ Nachricht zeigen
    macpassword_try = raw_input('Geben Sie den Macpassword:')
    hash_macpassword_try = hashlib.sha512(macpassword_try).hexdigest()

    lauf = ord(decrypt_datei[0])
    mac_decrypt = ''
    i = 1
    while i <= lauf:
        mac_decrypt += decrypt_datei[i]
        i += 1

問題大概出在:

lauf = ord(decrypt_datei[0])

ord返回一個整數,表示字符的 Unicode 代碼點。

如果您有ord('a')將返回97因此如果您的字符串decrypt_datei包含'a'並且len(decrypt_datei)小於97它將導致string index out of range

我懷疑這里有邏輯錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM