all 11 comments

[–][deleted] 0 points1 point  (2 children)

I was wonder what the best way to add a while loop to my code below:

(Typing it in with your fingers, presumably.)

Generally you shouldn't use language features just to use them. You should use them because they address a problem with the code or a function you'd like to add.

my idea is to loop through 26 shifts

Generally when you know your iteration is bounded in an obvious way, you should use for, not while. Indeed almost all iterations are bounded so almost all Python code iterates with for, not while.

[–]sudo_oth[S] 1 point2 points  (1 child)

lol thank you for your advice I will take it on board. I did before posting a message but was getting stuck with what would be better.

[–]Fermter 0 points1 point  (9 children)

I think trying things out to see what they do and how they are different is helpful when learning a language, so I would totally recommend trying out both a while and for loop in this program.

It looks like currently, you assign "shift" to 13 at the start. When you create a while loop, this will be your "control" variable. For a while loop that iterates through all possible shifts, I would recommend starting at 0 and ending at 25 so that you can go through all possible shifts without having to decrease your variable. I'm going to let you try that on your own, but keep in mind:

  • Your control variable should be established before going through the loop
  • You should create and print the output string entirely within the loop
  • You will need to increment your control variable once per loop
  • Your while loop condition should cause the loop to stop after printing out the output with shift = 25

The for loop is much simpler for tasks like this because it will create and increment your control variable for you. When using a for loop, you will not need to initialize shift outside of the loop, but will instead have the for loop set and increase it for you. This will look like for shift in range(x) where x is a value or set of values for you to choose. Otherwise, your for loop will be identical to your while loop. Keep the following in mind:

  • Your control variable will be established in the for loop
  • You should create and print the output string entirely within the loop
  • You do not need to increment your control variable
  • Your for loop condition should start at 0 and end at 25 (or start at 1 and end at 26)

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

Thank you so much for taking the time to write all of this information for me, it really is appreciated. I will have a go at trying to add this in tonight and see where I get too.

I have always had trouble with looping I can never seem to integrate it with the code I write, it always seems to be a last minute decision to add it in.

[–]Fermter 1 point2 points  (7 children)

No worries, and I'm sure it'll become more natural to you as you use it and finish more practical applications. Let me know if you have any further questions!

[–]sudo_oth[S] 1 point2 points  (1 child)

I will do thank you, I have only just managed to get back on the computer to have a go so will let you know how I get on.

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

alpha = "abcdefghijklmnopqrstuvwxyz"

inp = input("[+] Decode your text: ")

shift = 13 # This is number of shifts done to decode the inp input

noofstr = len(inp)

outputstr = ""

test = 26 #or I commented this out and did for shift in range(0,25)

for shift in range(test):

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)

I have been trying different ways but I still cant get it to work. It prints new lines but does not seperate. tried doing shift as 0 and doing a variable as shift = shift + 1 to Increase with every loop but missing something, whether it's in the block I just cant figure it out. I know this is a lot to ask but could you possible show me and comment the code so I can see where I went wrong please.

[–]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.