
Вот код, заранее спасибо за помощь.
from cs50 import get_string
import sys
def main():
# Check usage and key
if len(sys.argv) != 2 or not sys.argv[1].isalpha():
print("Usage: python vigenere.py k")
sys.exit(1)
# Prompt user for plaintext
plaintext = get_string("plaintext: ")
# Cipher text
print("ciphertext: ", end="")
# Set keyword iteration counter
i = 0
key = sys.argv[1]
len_key = len(key)
keyword = shift(key)
for c in plaintext:
# Take care of uppercase character
if c.isupper():
print(chr(((ord(c) - 65 + keyword) % 26) + 65), end="")
# Take care of lowercase character
elif c.islower():
print(chr(((ord(c) - 97 + keyword) % 26) + 97), end="")
# Take care of non alphabetic character
else:
print(c, end="")
# Take care of keyword counter
if i < len_key - 1 and plaintext.isalpha():
i = i + 1
keyword = shift(key)
elif i == len_key - 1 and plaintext.isalpha():
i = 0
keyword = shift(key)
print()
# Function shift value of each carachter of keyword
def shift(char):
if char.isupper():
value = ord(char) - 65
else:
value = ord(char) - 97
return value
if __name__ == "__main__":
main()
Подробнее здесь: https://stackoverflow.com/questions/592 ... betic-char
Мобильная версия