1
我想使用偶數和奇數的定義來加密我的消息。 功能def swap_letters(message)
是我使用的是什麼:Python - 消息加密返回`NoneType`錯誤
- 我遍歷的奇怪的名單,甚至與'「的消息=消息+‘X’」'信;
- 然後,我給
"for counter in range(0, int(len(message)/2)): ...
添加下一個奇怪的字母給最後的消息; - 最後,我在我的
new_message
通過加入letter_1
添加使用"letter_1.append(even_letters[counter])...
我作出最後的消息下一個偶數字母,然後返回它
我做什麼錯誤,我應該怎麼解決這個問題代碼讓它正確運行?
import tkinter as Tk
from tkinter import messagebox, simpledialog, Tk
def is_even(number):
return number % 2 == 0
def get_even_letters(message):
even_letters = []
for counter in range(0, len(message)):
if is_even(counter):
even_letters.append(message[counter])
def get_odd_letters(message):
odd_letters = []
for counter in range(0, len(message)):
if not is_even(counter):
odd_letters.append(message[counter])
return odd_letters
def swap_letters(message):
letter_1 = []
if not is_even(len(message)):
message = message + 'x'
even_letters = get_even_letters(message)
odd_letters = get_odd_letters(message)
for counter in range(0, int(len(message)/2)):
letter_1.append(odd_letters[counter])
letter_1.append(even_letters[counter])
new_message = ''.join(letter_1)
return new_message
def get_message():
message = simpledialog.askstring('Message', 'Enter the secret here: ')
return message
def get_task():
task = simpledialog.askstring('Task', 'Do you want to encrypt or decrypt?')
return task
root = Tk()
while True:
task = get_task()
if task == 'encrypt':
message = get_message()
encrypted = swap_letters(message)
messagebox.showinfo('Ciphertext of the secret message is:', encrypted)
elif task == 'decrypt':
message = get_message()
decrypted = swap_letters(message)
messagebox.showinfo('Plaintext of the secret message is:', decrypted)
else:
break
root.mainloop()
這是錯誤我得到:
Traceback (most recent call last):
File "T:\Sublime Text 3\shhhhh.py", line 47, in <module>
encrypted = swap_letters(message)
File "T:\Sublime Text 3\shhhhh.py", line 29, in swap_letters
letter_1.append(even_letters[counter])
TypeError: 'NoneType' object is not subscriptable
[Finished in 21.2s with exit code 1]
[shell_cmd: python -u "T:\Sublime Text 3\shhhhh.py"]
[dir: T:\Sublime Text 3]
[path: C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files `(x86)\Python36-32\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Xoreax\IncrediBuild;C:\Users\mwliv\AppData\Local\Microsoft\WindowsApps;]`
非常感謝你,這是超級有益的和非常簡單的,豎起大拇指從我! (老實說,這是一個愚蠢的疏忽......) –