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

时间:2015-04-24 14:08:49

标签: python python-2.7 int

我需要确保cypher的输入移位是整数。我如何确保它是这样的,到目前为止,当我在任何输入中使用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)

1 个答案:

答案 0 :(得分:2)

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