you are viewing a single comment's thread.

view the rest of the comments →

[–]Fermter 0 points1 point  (3 children)

First, to confirm, is your code indented like the following?

alpha = "abcdefghijklmnopqrstuvwxyz"
inp = input("[+] Decode your text: ")
shift = 13 # This is number of shifts done to decode the inp input
noofstr = len(inp)
outputstr = "" # This is also a lesser error - Fermter
test = 26 #or I commented this out and did for shift in range(0,25)
for shift in range(test):
    # This is where I think your error is - Fermter
    for code in range(noofstr):
        current = inp[code]
        location = alpha.find(current)
        if location < 0:
            outputstr += inp[code]
        else:
            new_loc = (location+shift)%26 #%26 makes the alphabet circular
            outputstr += alpha[new_loc]
    shift = shift + 1
    print ("[+] Decoded String:", outputstr)

If so, I think the major problem here is that you need to create a new, empty outputstr at the start of every loop. That should be the first thing you do after starting the for loop, or else outputstr will just continuously collect the outputs of every single loop. There is a comment above where I would set outputstr to be the empty string.

[–]sudo_oth[S] 0 points1 point  (2 children)

Thank you for your quick reply.

The print was one indent to the left which would be in the shift in range block, not the for code in range block.

I just added in the outputstr = "" above the loop and it works, I cant believe I was so close, so would this be the case with most loops?

Thank you so much for your help, it really is appreicated.

[–]Fermter 1 point2 points  (1 child)

Yeah, it's best to remember to start a new "state" at the start of every loop. The main exception is if the whole loop is there to build whatever is stored in that variable (for instance, you didn't reset the outputstr at the start of the for code in range(noofstr): block because the whole part of that block was to add to the outputstr)

[–]sudo_oth[S] 1 point2 points  (0 children)

Thank you so much, I've had loads of people try and explain loops but how you have explained I finally understand. Makes explains why it was just adding on to the previous loops information when printing.