python 加密程序中 int() 的问题

problems with int() in a python encryption programm

我需要确保密码的输入移位是一个整数。到目前为止,当我对任何输入使用 int() 变量时,我将如何确保它是正确的,即使我确实输入了整数,它也会产生错误。

这里是代码,是用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)

对于 python 2.7,您想使用 raw_input 而不是 input。有关详细信息,请参阅 this post