2017-05-15 1 views
0

암호화 및 암호 해독 시스템을 만들려고했지만 약간의 오류가 발생했습니다.할당 전에 로컬 변수를 참조 할 수 있습니다 - Python

import sys 
    import pyperclip 


    def copy(data): 
     question = input("Copy to clipboard? ") 

     if question.lower() == 'yes' or question.lower() == 'y': 
      pyperclip.copy(data) 
      print("Encrypted message copied to clipboard.") 
      rerun() 

     elif question.lower() == 'no' or question.lower() == 'n': 
      rerun() 

     else: 
      print("You did not enter a valid input.") 
      copy(data) 


    def rerun(): 
     ask = input("\nWould you like to run this program again? ") 

     if ask.lower() == "yes" or ask.lower() == "y": 
      print(" ") 
      run() 

     elif ask.lower() == 'no' or ask.lower() == 'n': 
      sys.exit("\nThank you!") 

     else: 
      print("You did not enter a valid input.") 
      rerun() 


    def encrypt(key, msg): 
     encrypted_message = [] 
     for i, c in enumerate(msg): 
      key_c = ord(key[i % len(key)]) 
      msg_c = ord(c) 
      encrypted_message.append(chr((msg_c + key_c) % 127)) 
     return ''.join(encrypted_message) 


    def decrypt(key, encrypted): 
     msg = [] 
     for i, c in enumerate(encrypted): 
      key_c = ord(key[i % len(key)]) 
      enc_c = ord(c) 
      msg.append(chr((enc_c - key_c) % 127)) 
     return ''.join(msg) 


    def run(): 
     function_type = input("Would you like to encrypt or decrypt a message? ") 

     if function_type.lower() == "encrypt" or function_type.lower() == "e": 
      key = input("\nKey: ") 
      msg = input("Message: ") 
      data = encrypt(key, msg) 
      enc_message = "\nYour encrypted message is: " + data 
      print(enc_message) 
      copy(data) 

     elif function_type.lower() == "decrypt" or function_type.lower() == "d": 
      key = input("\nKey: ") 

      question = input("Paste encrypted message from clipboard? ") 

      if question.lower() == 'yes' or question.lower() == 'y': 
       encrypted = pyperclip.paste() 
       print("Message: " + encrypted) 

      elif question.lower() == 'no' or question.lower() == 'n': 
       encrypted = input("Message: ") 

      else: 
       print("You did not enter a valid input.") 
       run() 

      decrypted = decrypt(key, encrypted) 
      decrypted_message = "\nYour decrypted message is: " + decrypted 
      print(decrypted_message) 
      copy(decrypted) 

     else: 
      print("\nYou did not enter a valid input.\n") 
      run() 

    run() 

그것은 지역 변수 '암호화'할당하기 전에 참조 할 수와 하이라이트 실행() 함수에서

decrypted = decrypt(key, encrypted) 

말한다 : 여기 내 코드입니다.

다른 함수에서 'encrypted'변수를 사용했기 때문에 그것이 가능합니까? 그렇다면 어떻게 수정하고 내 프로그램의 기능을 유지할 수 있습니까?

나는 당신이 당신의 대답을 설명 할 수 있다면 나는 그것을 고맙게 생각할 것이기 때문에 비교적 새로운 것이다.

답변

1

이 추가 run() 전에

+0

으로 if 조건 중 하나라도하기 전에 변수를 초기화 할 수 있습니다. 고맙습니다! – PythonPie

8

else 분기가 실행되면 encrypted이 정의되지 않습니다. IDE가 run()에 다시 전화하는 것을 알지 못합니다. 다른 제어 흐름 메커니즘을 사용해야합니다 있도록이 무한 재귀가 발생할 수 있습니다 염두에

에 보관할 encrypted = None

0

(입력이 유효 할 때 나누기 while 루프를 사용해보십시오) 경고에 의해 생성 된 경고. 린터가 encrypted 조건

if question.lower() == 'yes' or question.lower() == 'y': 

및 그러나

elif question.lower() == 'no' or question.lower() == 'n': 

에서, 린터이 두 조건이 서로 complementry 경우 있음을 알 수 없다이 경우 내부 값이 할당되어 있음을 볼 수 있기 때문에

입니다 . 따라서 어떤 조건도 참이 아닌 경우를 고려하면 변수 encrypted은 초기화되지 않은 상태가됩니다.

은이 경고를 없애려면, 당신은 단순히 지금 작동하는 것 같다 None

관련 문제