Using while True miltiple times

Viewed 62

I'm new here and this is my first question. Apologies for any shortcomings.

I'm trying practise using a simply code. This example has the below output:

*who are you?  
Joe  
Hello Joe. What is the password? (It is a fish)  
hoki  
Access granted  
Hello Joe. What is the password? (It is a fish)*  

The code I am using is:

while True:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        print('Access granted')*

As im trying to get out of the second while True loop using the continue statement (if the answer to the password question is hoki) it asks again "Hello Joe. What is the password? (It is a fish)* It should be ending and not asking me for the password again as I've entered the correct one.

Any advice is appreciated. Thanks

6 Answers

Here's the solution

user_check = True
while user_check:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        else:
            print('Access granted')
            user_check = False
            break

You can use a break statement in your code to end the loop.

while True:
    print('This loop will stop because of my break statement!')
    break

print('This will print out because we used the break statement!')

Problem with your code is that it is just using continue. There is no statement for program to break out of the while-loop.

You just need two break statements, like this:

while True:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        print('Access granted')
        break # break inner loop
    break # break outer loop

Like some others said you should use break instead of continue but you'll also need to change your password check to be `password == 'hoki'

  1. remove the * before the first while statement, and after the print('Access granted)
  2. indent all code that follows so that it is executed within the while loop.
  3. finally, add break statements as per below:
while True:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        print('Access granted')
        break
    break

This should give the desired outcome. A more "semantic" way of solving the problem might be this:

logged_in = False

while not logged_in:
    print('who are you?')
    name = input()
    if name == 'Joe':
        while not logged_in:
            print('Hello Joe. What is the password? (It is a fish)')
            password = input()
            if password =='hoki':
                print('Access granted')
                logged_in = True

# Code only accessible to logged-in user
print("Now let's do some things that only Joe can do...")

Hope this helps!

Instead of using a While True directly; make it While (variable). Then proceed with your logic. See example below:

a = True

while a:

     # apply logic
     # if you want to break this loop; assign False to a.

     b = True

     while b:

         # apply logic
         # if you want to break this loop; assign False to b


         # .... and so on

Hope this is helpful.

Related