krekeren
krekeren

Reputation: 1

loops;end statement in python

I have this assignment: - Write an error-trap condition loop that asks the user for username and password inputs iteratively, until correct values are entered.

this is what I wrote so far:

while x in range(2):
    x = x + 1
    if  q1==username and q2==password:
       print("you entered the enchanted palace")
       break
    else:
       print("wrong username/password, try again")
       q1 = input("enter username: ")
       q2 = input("enter password: ")

I really don't know how to approach the third clause.

Upvotes: 0

Views: 63

Answers (1)

Prune
Prune

Reputation: 77827

You're so close:

  1. Loop 3 times, rather than twice.
  2. Replace the while and increment with for x in range(3):
  3. Set a flag so you can tell whether or not you got a valid password. If not, print that final message.

WIth a couple of simple code updates ...

valid = False

for tries in range(3):
    if  q1==username and q2==password:
       print("you entered the enchanted palace")
       valid = True
       break
    else:
       print("wrong username/password, try again")
       q1 = input("enter username: ")
       q2 = input("enter password: ")

if not valid:
    print("wrong username/password, contact a system administrator”)

Upvotes: 2

Related Questions