Home > Software engineering >  Why does this Python program not let me type a Code-Word that's behind the last one I typed in
Why does this Python program not let me type a Code-Word that's behind the last one I typed in

Time:09-17

I am making a decoder, but when I type a code-word, if the code-word is not after the last one I typed in the list, it throws an error

Code:

CodeNum = 0
while RTs < 9:
        CodeWord = str(input("Code-word: "))
        Stop = ["Stop", "Exit", "End", "Terminate"]
        if CodeWord in Stop:
            break
        while CodeNum < 55:
            if CodeWord == CodeWordsUpC[CodeNum]:
                output.append(LettersUpC[CodeNum])
                break
            else:
                CodeNum = CodeNum   1
        RTs = RTs   1

Error:

Traceback (most recent call last):
  File "/home/pi/mu_code/Encoder_Decoder.py", line 97, in <module>
    if CodeWord == CodeWordsUpC[CodeNum]:
IndexError: list index out of range
LettersUpC = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O",
              "P", "A", "S", "D", "F", "G",
              "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "<",
              ">", "_", ": ", "-"]

CodePudding user response:

What I was forgeting is that I needed to reset 'CodeNum' to 0

output = [":", ]
RTs = 0
InOrOut = str(input("Encode or Decode, E/D: "))
if InOrOut == "D":
    while RTs < 9:
        CodeNum = 0
        CodeWord = str(input("Code-word: "))
        Stop = ["Stop", "Exit", "End", "Terminate"]
        if CodeWord in Stop:
            break
        while CodeNum < 55:
            if CodeWord == CodeWordsUpC[CodeNum]:
                output.append(LettersUpC[CodeNum])
                break
            else:
                CodeNum = CodeNum   1
        RTs = RTs   1
    print(str(output[1]   output[2]   output[3]
                output[4]   output[5]   output[6]
                output[7]   output[8]))

Now it works, thanks Karl Knetchel!

  • Related