0

I was wondering if someone here could help better explain For loops than my textbooks.

I'm writing a program in Python where the program askes a user if they're already an existing user. If they are an existing user, the program starts. If they're not an existing user, they're prompted to create a username and password.

I was wondering if someone could help explain how I could use a For Loop to loop the if statement incase a user enters some unexpected input like A or a number.

Thanks

Here's a code snippet of an if statement I have where it goes:

if newuser == "y":
    print("Hello New User")
elif newuser == "n":
    print("Hello Existing User")
else:
    print("Please try again")
1
  • 2
    You want a while loop for this, not a for loop. A for loop steps a variable through an iterable, which doesn't apply to your situation since there is no iterable. Commented Jun 13, 2021 at 20:26

1 Answer 1

2

Try creating a while loop, and only break out of this if a condition is met.

while True:
    newuser = input('enter y/n')
    if newuser == "y":
        print("Hello New User")
        break
    elif newuser == "n":
        print("Hello Existing User")
        break
    else:
        print("Please try again")
        
Sign up to request clarification or add additional context in comments.

Comments

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.