Need help with looping

Hi, I am new to python and need some help with a simple loop. I want the code to loop this section input("You are in the Lost Forest\n****************\n****** \n (╯°□°)╯︵ ┻━┻\n*************\n****************\nGo left or right? ") until the user inputs left(or something other than right). I can do that on it’s own but somehow it’s interactions with my counter is not continuing to loop after the counter ends. I’ve tried varying combinations of nested loops and can’t seem to get it quite right. Any help would be greatly appreciated!

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? “)
while n == “right” or n == “Right”:#makesureyoudupe n==”"bothsidesofor
for n in range(4):
input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
input("You are in the Lost Forest\n****************\n****** \n (╯°□°)╯︵ ┻━┻\n*************\n****************\nGo left or right? ")
print(“You got out of the Lost Woods\o/”)

It’s impossible to understand your code with the indentation missing like this (hint: put ``` before and after your code to make a code block in discourse), but

  • you appear to be using the variable n for two different things and
  • did you mean to ignore the return value of the 2nd and 3rd calls to input?
1 Like

lostwoods1

Here is an image depicting the code a little clearer. It does everything(prints first input, prints second input 4 times, prints 3rd input) except when I continue typing right after the 3rd input it just prints out the final line(6). Where as I would like the code to continue showing input 3(line5) until the user inputs something other than “right” or ‘Right’.

Thanks again for the help.

Think about all the places you are assigning (or not assigning) the variable n.

(and don’t post code as images, unless of course you edit your code with Gimp or Photoshop. Not everyone here can see them, your code is cut of at the right hand edge of your screen, and some rare mistakes aren’t visible in a screenshot)

1 Like

Okay. I’m unsure how to use acute ticks to do that right now sorry. I will work on it. I added n = input() to line 5 and it loops the whole program again but I want it to continue putting input(line5). Also, if I put left in on any input except line 5. It won’t break the loop. I guess because it’s still in the listing process of the for loop. I may need a break statement somewhere. I’ll keep working on it. Anymore suggestions are still appreciated.

Here is how I got it to perform all the operations I wanted. If the second while loop is redundant or unnecessary please let me know.

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
while n == "right" or n == "Right":#makesureyoudupe n==""bothsidesofor
    for n in range(4):
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
    while n == "right" or n == "Right": 
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("You got out of the Lost Woods\o/")

First, the triple backticks worked nicely. Remember to put some more
below the code too.

To the code, assorted remarks inline below:

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")

I’d write this like so:

print("You are in the Lost Forest*")
print("***************\n****************")
print(":")
print("****************\n****************")
n = input("Go left or right? ")

Separates the preamble from the prompt, and makes it easier to read.

while n == "right" or n == "Right":#makesureyoudupe n==""bothsidesofor

Put a few spaces before the comment:

while n == "right" or n == "Right":  #makesureyoudupe n==""bothsidesofor

I found it hard to see.

    for n in range(4):
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")

This loop asks for a direction unconditionally 4 times. You don’t test
the response in any way, so the loop proceeds to ask again.

    while n == "right" or n == "Right":
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")

Then after that, this loop seems to ask repeated until you do not get
a “right” or “Right”. I doubt that is what you intend. A more normal
setup might be:

    for n in range(4):
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
        if n == "right" or n == "Right":
            break
        print("go right!")

Finally:

print("You got out of the Lost Woods\o/")

Hooray!

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

I am struggling with testing that for loop throughout it’s iterations. Can’t seem to get it to break like I would like. I’m wanting it where someone only needs to type anything other than right(ex. left) at any point during the program to break any current loop and jump to end printing out (“You got out of the lost woods”). Otherwise I would like it print line 1 input text once. Line 5 input text 4 times. Then line 9 input text until they type something other than right.

Something along these lines maybe? I know this isn’t correct and doesn’t function as intended but still seems closer than my mostly operation code.

Thank you for the input on formatting the preamble and notation also! I added the spacing to notation but will wait on preamble spacing for now.

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
while n == "right" or n == "Right": #makesureyoudupe n==""bothsidesofor
    for n in range(4):
        if n == "right" or n == "Right":
            n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
        else:
            print("You got out of the Lost Woods")
    while n == "right" or n == "Right": #makesureyoudupe n==""bothsidesofor
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("You got out of the Lost Woods\o/") 

This ended up working for what I was wanting.

n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
while n == "right" or n == "Right": #makesureyoudupe n==""bothsidesofor
    for n in range(2):
        n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
        if n == "right" or n == "Right": #makesureyoudupe n==""bothsidesofor
            n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ") 
        else:
            print("You got out of the Lost Woods\O/")
            exit("End")
    while n == "right" or n == "Right": #makesureyoudupe n==""bothsidesofor
        n = input("You are in the Lost Forest\n****************\n******       ***\n  (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
print("You got out of the Lost Woods\O/")

The only concern I have is using exit() to terminate the program.

        else:
            print("You got out of the Lost Woods\O/")
            exit("End")

I have read it is not to be used in production code. Plus it throws up a traceback error after everything has finished but without it I couldn’t figure out a decent control flow to keep from printing the exit text twice. If there are better alternatives please let me know so I can implement those in future exercises and projects. Thank you both.

This ended up working for what I was wanting.

Good.

while n == “right” or n == “Right”: #makesureyoudupe n==""bothsidesofor

You can also write this as:

n in ("right", "Right")

or even:

n.lower() == "right"

for n in range(2):
n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? “)
if n == “right” or n == “Right”: #makesureyoudupe n==”"bothsidesofor
n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
else:
print(“You got out of the Lost Woods\O/”)
exit(“End”)

Don’t do this! This cancels the whole programme. If you want to exit the
look, use:

break

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

This if statement will never see a True value: in the for n in ... loop, the variable n is reassigned on every iteration. Whatever n is before the loop, inside it will first be set to 0, then 1, and so on. It will never be equal to "right".

It might be easier for you to reason about your code and to understand what it’s doing if you used a different name for the loop variable and the user’s answer.

1 Like

If I use break there it jumps me down to the last print(), printing the ending statement twice. So this was all I could come up with for now to avoid that.

I think I see what you mean (0-3) on iterations of the for loop but when typing left anywhere during the loop instead of right, it does jump to the else statement and ends() program. Otherwise it runs through the if condition fine with no issues. I’m trying to run trace so maybe I can have a better understanding of what’s going on. I’ll look into that today.

No luck in any capacity as far as this substitution goes. Break worked fine once removing the first print statement though. Thank you.

You may want to use separate variables for the loop counter (n) and the
answer to the input (some other name).

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like