2

So I am new at programming and I was writing some practice code (Python 3.6):

while True:
 print('Hello Steve, what is the password?')
 password = input()
 if password != '1234':
 continue
 print('Access granted')

The problem i'm having is that even though I am typing the correct password, the loop continues.Can you help me figure out what I did wrong?

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jun 24, 2017 at 13:54
3
  • 6
    use break instead of continue Commented Jun 24, 2017 at 13:55
  • Or to expand on that, the while True loop can only be exited with a break statement Commented Jun 24, 2017 at 13:58
  • What exactly do you want to happen? The code is not very clear about that. Commented Jun 24, 2017 at 13:58

4 Answers 4

8

continue will skip the rest of the current round in the loop, and then the loop will start over:

>>> i = 0
>>> while i < 5:
... i += 1
... if i == 3:
... continue
... print(i)
...
1
2
4
5
>>>

What you're looking for is the break keyword, which will exit the loop completely:

>>> i = 0
>>> while i < 5:
... i += 1
... if i == 3:
... break
... print(i)
...
1
2
>>>

However, notice that break will jump out of the loop completely, and your print('Access granted') is after that. So what you want is something like this:

while True:
 print('Hello Steve, what is the password?')
 password = input()
 if password == '1234':
 print('Access granted')
 break

Or use the while loop's condition, although this requires repeating the password = ...:

password = input('Hello Steve, what is the password?\n')
while password != '1234':
 password = input('Hello Steve, what is the password?\n')
print('Access granted')
answered Jun 24, 2017 at 13:57
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your detailed response!
Last ones gives NameError: name 'password' is not defined.
And now the user must input something but isn't told that they must do so.
@StefanPochmann oh master, show me how to do/while in Python. Like I already mentioned, "although this requires repeating the password = ...". There's no perfect do/while in Python, you either while True: (which I've shown) or repeat the input (which I've shown). If you have a better method, feel free to enlighten us ;)
@MarkusMeskanen Simply initialize with password = None instead.
|
0

Change break instead of continue, should work.

answered Jun 24, 2017 at 13:57

2 Comments

So, there is no way to do this with continue?Sorry for the stupid questions i'm quite new at this.
use break, it is provided for that reasn only to stop the loop, it is opposite of continue
0

First of all you're using the wrong logical operator for equality comparison, this: != is for not equals; and this == is for equals.

Second, as other have already stated, you should use break instead of continue.

I would do something like this:

print('Hello Steve!')
while True:
 password = input('Type your password: ')
 if password == '1234':
 print('Access granted')
 break
 else:
 print('Wrong password, try again')
answered Jun 24, 2017 at 14:17

Comments

0

Try using break statement instead of continue. Your code should look like this

while True:
 print('Hello Steve, what is the password?')
 password = input()
 if password == '1234':
 print('Access granted')
 break
answered Apr 20, 2020 at 0:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.