2014-07-07 8 views
0
import sys 
alphabet = ("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") 
rotor1 = ("p","l","m","k","o","n","j","i","b","h","u","v","g","y","c","f","t","x","d","r","z","s","e","a","w","q") 
rotor2 = ("e","b","h","r","k","a","s","t","i","u","m","z","g","y","q","v","d","l","c","x","n","w","o","p","f","j") 
rotor3 = ("test") 
rotors = (alphabet,rotor1,rotor2,rotor3) 
reflector = ("test") 

def menu(): 
    print "Welcome to the Enigma machine!" 
    print "------------------------------" 
    print "1) Encrypt your message" 
    print "2) Change the connections on the plugboard" 
    print "3) Exit the program" 
    userchoice = raw_input("Please choose an option") 

    if userchoice == "1": 
     encrypt() 
    elif userchoice == "2": 
     plugboard() 
    elif userchoice == "3": 
     sys.exit() 

def encrypt(): 
    alphapos = [] 
    rotor1pos = [] 
    encryptedword = [] 
    userinput = raw_input("Please enter the message that needs to be encrypted") 
    usermsglist = list(userinput) 
    for x in range(0,2): 
     for i in range(0,len(usermsglist)): 
      alphapos.append(rotors[x].index(usermsglist[i])) 
      encryptedword.append(rotor1[alphapos[i]]) 
      usermsglist = ''.join(encryptedword) 
    print usermsglist 

나는 내 코드를 내 enigma 기계에 더 효율적으로 사용하기 위해 for 루프를 만들려고 노력하고있다. 내가목록에서 파이썬으로 목록 내부에 추가하기

내가 오류를 얻을 목록 알파벳에게 인 로터리스트 내 항목에서 추가 할 때 나는 오류가

IndexError: string index out of range 

이 오류를 구체적으로 로터를 일으키는 코드의 라인은 [이다 X] 부분하지만 난이 프로그램을 알파벳을 변경하면 잘 작동 : 모든

alphapos.append(rotors[x].index(usermsglist[i])) 
+2

A [최소한의 예]를 입력하십시오 (HTTP 만 0 번째에는 첫 번째 요소가 없습니다 ://stackoverflow.com/help/mcve), 올바른 형식으로되어 있으며 전체 오류 추적입니다. – jonrsharpe

+1

여기서는 rotor3입니까? –

+1

오류가 발생한 행은 무엇입니까? –

답변

1

문제는 색인 관리입니다. enumerate을 사용하기 위해 루프를 변경했으며 코드 오류없이 결과를 얻었습니다.

import sys 
alphabet = ("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") 
rotor1 = ("p","l","m","k","o","n","j","i","b","h","u","v","g","y","c","f","t","x","d","r","z","s","e","a","w","q") 
rotor2 = ("e","b","h","r","k","a","s","t","i","u","m","z","g","y","q","v","d","l","c","x","n","w","o","p","f","j") 
rotor3 = ("test") 
rotors = (alphabet,rotor1,rotor2,rotor3) 
reflector = ("test") 

def menu(): 
    print "Welcome to the Enigma machine!" 
    print "------------------------------" 
    print "1) Encrypt your message" 
    print "2) Change the connections on the plugboard" 
    print "3) Exit the program" 
    userchoice = raw_input("Please choose an option") 

    if userchoice == "1": 
     encrypt() 
    elif userchoice == "2": 
     plugboard() 
    elif userchoice == "3": 
     sys.exit() 

def encrypt(): 
    alphapos = [] 
    rotor1pos = [] 
    encryptedword = [] 
    userinput = raw_input("Please enter the message that needs to be encrypted") 
    usermsglist = list(userinput) 
    for x in range(0,2): 
     for i, s in enumerate(userinput): 
      alphapos.append(rotors[x].index(s)) 
      encryptedword.append(rotor1[alphapos[i]]) 
      usermsglist = ''.join(encryptedword) 
    print usermsglist 

결과 :

Please enter the message that needs to be encryptedhello 
iovvciovvc 


UPDATE :

코드를 좀 더 자세히보고 후, 당신이 마지막에usermsglist를 재 할당되어 있기 때문에 실패하는 것 같다 첫 루프 반복의.

루프 검사가 갑자기 변경되는 list입니다.

첫 번째 반복에서 usermsglist은 길이가 1 인 목록 인 encryptedword에 할당됩니다. 루프가 1의 값을 틱 때 usermsglist[1] 찾지 만 usermsglist['h'] 같다 (내 예제)와

+1

아, 감사합니다 도움을 주셔서 감사합니다. 나는 원래 코드를 msg = ''.join (encryptedword)으로 바꿨다. – user3813953

+0

기꺼이 도와 드리겠습니다 :) – NuclearPeon

-2

먼저, 오류는 rotor3 준비하지 않았다에서 온다.

rotor[x + 1]이 루프가 원인입니다. 사용 파이썬 dictionary 또는 ord 기능 :

다음으로, 효율적인 방법 ... 아니이 프로그램은 것 같습니다하지 않는 작업 ... ... 다시

힌트를 사용해보십시오.

+2

나는 downvote하지 않았지만 comment에 rotor3 오류가 언급되었습니다. 또한, 당신의 대답은 질문에 대답하고 있지 않지만, 비평은 코멘트 섹션에 남겨 두어야한다고 생각합니다. – NuclearPeon

+0

. 그러나 나는 전체 질문에 대답 할 필요가 없다고 생각합니다. –

+1

질문에 대답하지 않으면 downvoted 될 것으로 예상 ... –

관련 문제