1

Hello i am trying to create a small code that receives password attempts for a number of times (3) , and if the password is wrong , the user can't try again .

can you please tell me why my code is not giving me the expected results i want?

the results it is giving me is a loop that never ends to ask for a try again ; while te results i desire is a loop that ends after 3 trials only . "i would love to keep in the while loop"

thank you very much

my code is

password=''    
for i in range(1,3):
        if i<3:
           while password!='daniel':
                    password=input("enter the password:")
                    if password=="daniel":
                        print('your logged in ')
                    else:
                        print('try again ')
        else:
            print("number of trials done")

`
4
  • Not a solution, but when you use for i in range(1,3), including if i<3: is redundant. Similarly, while password!='daniel': makes if password=="daniel": redundant. It might be worth looking into loops, conditions, and the break statement again and rewriting this code Commented Feb 25, 2020 at 19:16
  • 2
    Please explain the "not working" part. What do you expect and what you actually get? Commented Feb 25, 2020 at 19:16
  • @G.Anderson - you may want to rethink some parts of your comments. while loop wouldn't end the very moment its condition becomes false. Commented Feb 25, 2020 at 19:19
  • @G.Anderson i have re-edited my question , if you can help me. thank you Commented Feb 25, 2020 at 19:44

5 Answers 5

1

When you run this code you should get the error: NameError: name 'password' is not defined. This is because Python runs through your code line by line, and when it gets to the line while password!='daniel' it has no idea what password is because it hasn't yet been told, and so just stops running.

To fix errors like this you just need to first tell Python that password is going to be a variable which will store text. You can initialise the variable with empty text like so: password='', this must be put anywhere before the line that causes the error.

Sign up to request clarification or add additional context in comments.

Comments

1
for i in range(3):
  password=input("enter the password:")
  if password=="daniel":
      print('your logged in ')
      break
  elif i < 2:
      print('try again ')
else:
  print('Number trials exceeded')

Comments

0

Using both for loop and nested while loop makes the number of trials has no end. In addition, you should assign the variable password before you add it to the condition, or you can use while True and break instead.

counter = 0
while True:
    if counter < 3:
        counter += 1
        password = input('Enter the password: ')
        if password == 'daniel':
            print("You're logged in")
            break
        else:
            print('Try again')
    else:
        print('Number of trials is done. Please try again later.')
        break

Or you can do it using for loop.

for i in range(0,4):
    if i < 3:
        password = input('Enter the password: ')
        if password == 'daniel':
            print("You're logged in")
            break
        else:
            print('Try again')
    else:
        print('Number of trials is done. Please try again later.')
        break

Comments

0

The first problem with you code is you are using password with out intializing it. second the while loop will run forever unless the correct password is given. third even with the correct password given the while loop has a chance to restart for another 2 rounds

Here is an alternative

for trial in range(3):
    password=input("enter the password:")
    if password=="daniel":
        print('your logged in ')
        break
    elif trial == 2:
        print("number of trials done")
    else:
        print('try again ')
    trial = trial + 1

Comments

0
for i in range(0,3):
    if i<3:
        password = input("enter the password:\n")
        if password=="daniel":
            print('your logged in ')
            break
        else:
            print('try again ')
            continue
    else:
        print("number of trials done")

Or

password=''
i = flag = 0
while password!='daniel'and  i<3:
    password=input("enter the password:\n")
    if password=="daniel":
        print('your logged in ')
        flag=1
    else:
        print('try again ')
    i +=1

if flag==0:
    print("number of trials done")

2 Comments

thank you but i would love to use the while loop to see , how it would work , if it is possible .
@KaberaDavid, I have edited the post with while loop solution

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.