yj311 发表于 2022-12-10 19:50:15

英文加解密器

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)
    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)
    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)
      letter_list.append(even_letters)
    new_message = ''.join(letter_list)
    return new_message

def get_task():
    task = input('Do you want to encrypt or decrypt?\n')
    return task

def get_message():
    message = input('Enter the secret message:\n')
    return message

#主循环
while True:
    task = get_task()
    if (task == 'encrypt'):
      message = get_message()
      encrypted = swap_letters(message)
      print('Ciphertext of the secret message is: ' + encrypted)
    elif (task == 'decrypt') :
      message = get_message()
      decrypted = swap_letters(message)
      print('Plaintext of the secret message is: ' + decrypted)
    else :
      break

yj311 发表于 2023-5-3 16:05:05

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)
    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)
    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)
      letter_list.append(even_letters)
    new_message = ''.join(letter_list)
    return new_message

def get_task():
    task = input('Do you want to encrypt or decrypt?\n')
    return task

def get_message():
    message = input('Enter the secret message:\n')
    return message

#主循环
while True:
    task = get_task()
    if (task == 'encrypt'):
      message = get_message()
      encrypted = swap_letters(message)
      print('Ciphertext of the secret message is: ' + encrypted)
    elif (task == 'decrypt') :
      message = get_message()
      decrypted = swap_letters(message)
      print('Plaintext of the secret message is: ' + decrypted)
    else :
      break
页: [1]
查看完整版本: 英文加解密器