-3

image of the executed codeThe code executes, but the print statements run twice. Can someone explain why is it so?

n = input('you are lost in the woods, turn left or right\n**********\n**********\n:)\n**********\n**********\n')
m = 0
if n == 'right':
    while n == 'right' and m < 2:
        n = input('you are lost in the woods, turn left or right:')
        m += 1
    print('**********\n***    ***\n (/⚬⎯○)/ ∐\n**********\n**********')
if n == 'left':
    print('You got out of the woods')

I want the first print statement to run, if the input is 'right' thrice. I want the second print statement to run, if the input is 'left'.

The problem is, if i enter 'right', the third time the print statement in the first if block is executing twice. If i enter 'left', the print statement in the second if block executes twice.

1 Answers1

1

here is the correct code:

n = input('you are lost in the woods, turn left or right\n**********\n**********\n:)\n**********\n**********\n')
m = 0
if n == 'right':
    while n == 'right' and m < 1:
        n = input('you are lost in the woods, turn left or right:')
        m += 1
    print('**********\n***    ***\n (/⚬⎯○)/ ∐\n**********\n**********')
if n == 'left':
    print('You got out of the woods')

m is 0 in the beginning and you did

while n == 'right' and m < 2: