im trying to make a age and name approval. When they have 0 chances they cant continue. My problem is when they get 0 they can still continue

Issue

password = "12345"
for i in range(4):
    pwd = input("ENTER YOUR PASSWORD : ")
    j = 3
    if(pwd == password):
        print("WELCOME!")
        break
    else:
        print("Not Valid, try again and chances left are",j-i)
        continue

Solution

If you want to continue even if it reaches 0 chance. Instead of using for loop use while loop instead. So until the requirements meet, it’s don’t stop.

    password = "12345"
while True:
    pwd = input("ENTER YOUR PASSWORD : ")
    j = 3
    if(pwd == password):
        print("WELCOME!")
        break
    else:
        print("Not Valid, try again")
        continue

with nested if to restart every time it reach zero

        password = "12345"
chances = 4
    while True:
        pwd = input("ENTER YOUR PASSWORD : ")
        j = 3
        if(pwd == password):
            print("WELCOME!")
            break
        else:
            print("Not Valid, try again")
            chances -= 1
            if(chances <= -1):
              print("Your chance depleted pls restart again")
              #restart chance
              chances += 4
            continue

output

enter image description here

Answered By – Jericho

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published