【发布时间】:2023-04-02 03:03:01
【问题描述】:
我正在尝试使用偶数和奇数定义来加密我的消息。
函数def swap_letters(message)
是我正在使用的:
- 我使用 `"message = message + 'x'"' 遍历奇数和偶数字母列表;
- 然后,我用
"for counter in range(0, int(len(message)/2)): ...
在最后一条消息中添加下一个奇数字母; - 最后,我使用
"letter_1.append(even_letters[counter])...
将下一个偶数字母添加到最终消息中
我通过加入letter_1
来创建我的new_message
,然后将其返回
我犯了什么错误,我应该如何修复此代码以使其正常运行?
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;]`
【问题讨论】:
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python – 消息加密返回“NoneType”错误 - Python技术站