-1

I want Else statement to be shown once if it's wrong. Please see the code which I tried.

lists = ['apple','grape','pineapple','orange']

password = "xyz"
def pass1():
    for i in lists:
        if i == password:
            print("Accepted!", "Password : " + i)
            break
        else:
            print("Password not found! Try again")
pass1()

Output:

Password not found! Try again
Password not found! Try again
Password not found! Try again
Password not found! Try again

Process finished with exit code 0
AMC
  • 2,642
  • 7
  • 13
  • 35
  • None of the items in `lists` matches the password, so the code is working as expected. Have you tried `return`-ing inside the `else` block? – Gino Mempin Mar 23 '20 at 23:42
  • 1
    Check out this answer: https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list - you don't need to iterate over your list. You can use the `in` keyword to test all values against your password criteria at once. – Jon Mitten Mar 23 '20 at 23:45

3 Answers3

0

If I understand your question correctly, you can do that by removing the else, if the loop ends and you didn't find the password in the list then it doesn't exist.

lists = ['apple','grape','pineapple','orange']

password = "xyz"
def pass1():
    for i in lists:
        if i == password:
            print("Accepted!", "Password : " + i)
            return
    print("Password not found! Try again")
pass1()

Another and more pythonic way

def pass2():
    if password in lists:
        print(print("Accepted!", "Password : " + lists.index(password)))
    else:
        print("Password not found! Try again")
pass2()

I don't see why you're not passing the password as a parameter?

Maybe consider doing it as follow

def pass3(password, lists):
    if password in lists:
        print(print("Accepted!", "Password : " + lists.index(password)))
    else:
        print("Password not found! Try again")

lists = ['apple','grape','pineapple','orange']
password = "xyz"
pass3(password, lists)
Marsilinou Zaky
  • 1,038
  • 7
  • 17
0

Actually you don't need looping if you just only want to check if the value in the lists, just try this:

def pass1():
    if password in lists:
        print("Accepted!", "Password : " + password)
    else:    
        print("Password not found! Try again")

but if you still want to iterating the list you can use return like this:

def pass1():
    for i in lists:
        if i == password:
            return print("Accepted!", "Password : " + i)
    return print("Password not found! Try again")

because if you not use return the last code still be printed even the password is true.

dhentris
  • 198
  • 1
  • 2
  • 10
0

Another way to do it by using else on for loop
else block is executed only when every item is exhausted:

lists = ['apple','grape','pineapple','orange']

password = "xyz"
def pass1():
    for i in lists:
        if i == password:
            print("Accepted!", "Password : " + i)
            break
    else:
        print("Password not found! Try again")
pass1()
ywbaek
  • 2,971
  • 3
  • 9
  • 28