我希望它加密一条消息,但是它只说是一个错误:
第47行,在 加密= swap_letters(消息)
第27行,在swap_letters中 letter_list.append(odd_letters [counter]) IndexError:列表索引超出范围
def swap_letters(message) :
letter_list = []
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 )-1 ):
letter_list.append(odd_letters[counter])
letter_list.append(even_letters[counter])
new_message = ' '.join(letter_list)
return new_message
while True:
task = get_task()
if task == "encrypt":
message = get_message()
encrypted = swap_letters(message)
messagebox.showinfo("Message encrypted is: ", encrypted)
elif task == "decrypt":
message = get_message()
decrypted = swap_letters(message)
messagebox.showinfo("Plaintext of the message is: ", decrypted)
else:
break
root.mainloop()
完整代码
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])
return even_letters
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_list = []
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_list.append(odd_letters[counter])
letter_list.append(even_letters[counter])
new_message = ' '.join(letter_list)
return new_message
def get_task():
task=simpledialog.askstring("Task","Do you want to encrypt or decrypt?")
return task
def get_message():
message=simpledialog.askstring("Message","Enter your message")
return message
root = Tk()
root.withdraw()
while True:
task = get_task()
if task == "encrypt":
message = get_message()
encrypted = swap_letters(message)
messagebox.showinfo("Message encrypted is: ", encrypted)
elif task == "decrypt":
message = get_message()
decrypted = swap_letters(message)
messagebox.showinfo("Plaintext of the message is: ", decrypted)
else:
break
root.mainloop()
我希望它对短语进行打乱,但是它显示IndexError:列表索引超出范围