0

I need to make sure that the input shift for the cypher is a integer. How would I go around making sure that it is, so far when ever I use the int() variable round any of the inputs, it generates an error even when I do put an integer in.

Here is the code, it is written and compiled with 2.7.6

 import re

def caesar(plain_text, shift):
    cipherText = ''
    for ch in plain_text:
      stayInAlphabet = ord(ch) + shift
      if ch.islower():
        if stayInAlphabet > ord('z'):
          stayInAlphabet -=26
        elif stayInAlphabet < ord('a'):
          stayInAlphabet += 26
      elif ch.isupper():
        if stayInAlphabet > ord('Z'):
          stayInAlphabet -= 26
        elif stayInAlphabet < ord('A'):
          stayInAlphabet += 26
      finalLetter = chr(stayInAlphabet)
      cipherText += finalLetter
    print(cipherText)
    return cipherText

selection = input ("encrypt/decrypt ")
if selection == 'encrypt':
  plainText = input("What is your plaintext? ")
  shift = (int(input("What is your shift? ")))%26
  caesar(plainText, shift)

else:
  plainText = input("What is your plaintext? ")
  shift = ((int(input("What is your shift? ")))%26)*-1
  caesar(plainText, shift)

1 Answers1

2

For python 2.7, you want to use raw_input instead of input. See this post for more detail.

Community
  • 1
  • 1
C.B.
  • 8,096
  • 5
  • 20
  • 34