我怎么能清理我的代码?

时间:2014-02-01 20:47:59

标签: python

我怎样才能清理我的python代码?我基本上想要摆脱这个巨大的If语句,并且需要知道如何缩短它,如果你改变任何代码,也可以向我解释代码是如何工作的。感谢

global alpha
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']  

print("Welcome to Caesar Cipher")
print("How to use Caesar Cipher : \n 1. Select a number from 1 - 26 \n 2. Enter a Sentence/Word/Phrase to encrypt \n 3. Read the encryption.")

def main():
  global un_coded
  global turn
  turn = input("Please enter a number between 1-26 \n")
  if int(turn) > 26 or int(turn) < 1:
    print("Please only enter a number between 1-26")
    main()
  global un_coded
  un_coded = input("Input the Word/Phrase/Sentence you would like to code : \n")
  global x
  x = len(un_coded)
  global coded
  coded = ""

def change():
  global x
  global turn
  global coded
  global un1_coded
  un1_coded = un_coded.upper()
  y = 0
  for p in range(0, int(x)):
    if un1_coded[y] == "A":
      z = 1
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "B":
      z = 2
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "C":
      z = 3
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "D":
      z = 4
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "E":
      z = 5
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "F":
      z = 6
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "G":
      z = 7
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "H":
      z = 8
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "I":
      z = 9
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "J":
      z = 10
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "K":
      z = 11
      letter = int(z) + int(turn)
      letter = int(letter) - 1
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "L":
      z = 12
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "M":
      z = 13
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "N":
      z = 14
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "O":
      z = 15
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "P":
      z = 16
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "Q":
      z = 17
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "R":
      z = 18
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "S":
      z = 19
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "T":
      z = 20
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "U":
      z = 21
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "V":
      z = 22
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "W":
      z = 23
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "X":
      z = 24
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "Y":
      z = 25
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == "Z":
      z = 26
      letter = int(z) + int(turn)
      letter = int(letter) - 27
      letter_one = alpha[int(letter)]
      coded += str(letter_one)
    elif un1_coded[y] == " ":
      coded += " "
    y = int(y) + 1

main()
change()
print("Your Code Is - ")
print(str(un1_coded))
print(str(coded))

1 个答案:

答案 0 :(得分:2)

您可以通过这种方式更改if语句。

if un1_coded[y].isupper():
    z = ord(un1_coded[y]) - 64
    letter = z + int(turn)
    letter = int(letter) - 1
    letter_one = alpha[int(letter)]
    coded += str(letter_one)
elif un1_coded[y] == ' ':
    coded += " "