1

Code:

 while True:
  print('who are you')
  name = input()
  if name != (str('joe')):
   continue
  print('hello joe whats the password?')
  if password == 'saltysea':
   break
print ('access granted')

the code gets stuck when i input joe: it keeps asking "who are you"

Edit: heres what i did to get the intended result

 password = '0'
while True:
  print('who are you')
  name=input()
  if name != 'joe':
   continue
  print('hello joe whats the password?')
  password=input()
  if password == 'saltysea':
  break

print ('access granted')
2
  • I cannot reproduce your error. Are you inputting exactly "joe"? As an aside, if name != (str('joe')) is redundant... 'joe' is already a string! Commented Apr 3, 2017 at 9:47
  • huh that's weird... ok i double checked that i posted the right code but i didn't sorry im new to this all but the second segment of code is the correct segment of code Commented Apr 3, 2017 at 9:54

1 Answer 1

1

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

(Compare TP)

So in your case, you are iterating from top of while to the continue statement over and over. If you want to stop loop from executing after the name is not joe, use break

Your code should look something like this:

   while True:
           name = input('who are you')
           if name != (str('joe')):
                   break
           else:
                   password = input('hello joe whats the password?')
                   if password == 'saltysea':
                   print ('access granted')
                   break
Sign up to request clarification or add additional context in comments.

1 Comment

I think the case is that the loop gets iterating over and over even if you input joe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.