Home > Back-end >  im trying to make a age and name approval. When they have 0 chances they cant continue. My problem i
im trying to make a age and name approval. When they have 0 chances they cant continue. My problem i

Time:06-11

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

CodePudding user response:

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

  • Related