Home python The Caesar cipher, the English alphabet

The Caesar cipher, the English alphabet

Author

Date

Category

On the program input is a line of text in the English language in which you want to encrypt all the words. Each word line should be encrypted using Caesar cipher (cyclic shift on the word length). Lowercase letters thus remain lowercase and uppercase – uppercase

.

The format of the input data
The input to the program served a string of text in English.

The format of the output data
The program should print the ciphertext in accordance with the conditions of the problem.

Note. Characters that are not in English letters are not changed.
entry
Day, mice. “Year” is a mistake!
Output
Gdb, qmgi. “Ciev” ku b tpzahrl!
My code is

def encrypt (text, s):
  result = ""
  for i in range (len (text)):
    char = text [i]
    if (char.isupper ()):
      result + = chr ((ord (char) + s-65) 26 + 65%)
    else:
      result + = chr ((ord (char) + s - 97) + 97% 26)
      return result
#check the above function
text = input (). split ()
s = 4
print (text)
print (str (s))
print (encrypt (text, s))

Answer 1, Authority 100%

You are trying to process the word as a symbol. Also, you are granting special. characters when calculating the length of the word. And return block else .

Example:

def encrypt (text: str) - & gt; str:
  words = []
  for word in text.split ():
    new_word = ''
    word_len = len ([c for c in word if c.isupper () or c.islower ()])
    for char in word:
      if char.isupper ():
        new_word + = chr ((ord (char) + word_len - 65)% 26 + 65)
      elif char.islower ():
        new_word + = chr ((ord (char) + word_len - 97)% 26 + 97)
      else:
        new_word + = char
    words.append (new_word)
  return '' .join (words)
text = 'Day, mice. "Year" is a mistake! '
print (encrypt (text))

stdout:

Gdb, qmgi. "Ciev" ku b tpzahrl!

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions