Python Cesar密码编码

Python Cesar密码编码,python,encryption,Python,Encryption,我正在编写代码,用Cesar密码在python中加密一个单词。最后两行有一个小错误: UnboundLocal error: Local variable codeword referenced before assignment 我是Python新手,仍然不熟悉这种类型的错误 我应该能够允许用户输入一个字进行加密或解密,程序将根据用户的选择执行该过程 def main(): #take user word and pass it to the function to encrypt it

我正在编写代码,用Cesar密码在python中加密一个单词。最后两行有一个小错误:

UnboundLocal error: Local variable codeword referenced before assignment
我是Python新手,仍然不熟悉这种类型的错误

我应该能够允许用户输入一个字进行加密或解密,程序将根据用户的选择执行该过程

def main():
  #take user word and pass it to the function to encrypt it
  userinput=str(input("Enter the word to encrypt:"))
  UserInput = userinput.upper()
  print("You entered:",UserInput)
  key=int(input("Enter the key shift:"))
  sendtoincrypt(UserInput,key) #send the word for incryption
  
def sendtoincrypt(UserInput,key):
  coded_char=0
  for each in UserInput:
    if each=='A': 
      coded_char=65+key
      jointfun(coded_char)
    elif each=='B': 
      coded_char=66+key
      jointfun(coded_char)
    elif each=='C': 
      coded_char=67+key
      jointfun(coded_char)
    elif each=='D': 
      coded_char=68+key
      jointfun(coded_char)
    elif each=='E': 
      coded_char=69+key
      jointfun(coded_char)
    elif each=='F': 
      coded_char=70+key
      jointfun(coded_char)
    elif each=='G': 
      coded_char=71+key
      jointfun(coded_char)
    elif each=='H': 
      coded_char=72+key
      jointfun(coded_char)
    elif each=='I': 
      coded_char=73+key
      jointfun(coded_char)
    elif each=='J': 
      coded_char=74+key
      jointfun(coded_char)
    elif each=='K': 
      coded_char=75+key
      jointfun(coded_char)
    elif each=='L': 
      coded_char=76+key
      jointfun(coded_char)
    elif each=='M': 
      coded_char=77+key
      jointfun(coded_char)
    elif each=='N': 
      coded_char=78+key
      jointfun(coded_char)
    elif each=='O': 
      coded_char=79+key
      jointfun(coded_char)
    elif each=='P': 
      coded_char=80+key
      jointfun(coded_char)
    elif each=='Q': 
      coded_char=81+key
      jointfun(coded_char)
    elif each=='R': 
      coded_char=82+key
      jointfun(coded_char)
    elif each=='S': 
      coded_char=83+key
      jointfun(coded_char)
    elif each=='T': 
      coded_char=84+key
      jointfun(coded_char)
    elif each=='U': 
      coded_char=85+key
      jointfun(coded_char)
    elif each=='V': 
      coded_char=86+key
      jointfun(coded_char)
    elif each=='W': 
      coded_char=87+key
      jointfun(coded_char)
    elif each=='X': 
      coded_char=88+key
      jointfun(coded_char)
    elif each=='Y': 
      coded_char=89+key
      jointfun(coded_char)
    elif each=='Z': 
      coded_char=90+key
      #if coded_char>90 #<<-- I will work on this later to make the iteration once the code exceed 90
        #rem=coded_char-90
      jointfun(coded_char)
    
def jointfun(coded_char):
  str (codedword)

  codedword = ''.join((codedword,coded_char))
  print("The coded word is:", codedword)

谢谢大家的帮助,我正在用工作计划回答我的问题

def main():
#take user word and pass it to the function to encrypt it
  userinput=str(input("Enter the word to encrypt:"))
  UserInput = userinput.upper()
  print("You entered:",UserInput)
  key=int(input("Enter the key shift:"))
  sendtoincrypt(UserInput,key) #send the word for incryption
  
def sendtoincrypt(UserInput,key):
  coded_char=[]
  newcode=0
  for each in UserInput:
    x=ord(each)
    newcode=x+key
    coded_char.append(newcode)
  displaycodednum(coded_char)
    
def displaycodednum(coded_char):
  print("The coded number is:", coded_char)
  processcodedword(coded_char)

def processcodedword (coded_char):
  coded_word=[]
  # for i, c in enumerate('test'):
   #print i, c
  for each, c in enumerate(coded_char):
    #y = chr(65)
    y=chr(c)
    coded_word.append(y)
  displaycodedword(coded_word)

def displaycodedword(coded_word):
  print("The coded word is:",*coded_word, sep='')

即使没有错误,str codedword本身也不会做任何事情。str codedword语句应该做什么?为什么要包含这些?我正在尝试将新的编码编号附加到列表中,然后我会使用每个编码编号找到其字符等价性,然后打印新的编码单词。仅供参考-如果您认为需要编写包含26个if/elifs的if语句,几乎总有更好的方法。在这种情况下,它可能会帮助你知道ordA==65,等等。谢谢@DavidBuck-你能给我发一个指向ordA==65的资源的链接吗?我是Python新手,需要了解它。