How to stop repeating lines in a while loop

Question:

my code:

def login():
    print("Welcome to David's and John's Tenant Management System!")
    print("(1)Admin (2)Customer (3)Exit")
    while True:
        if selection() == "1":
            print("f")  # admin login
        if selection() == "2":
            print("Are you an existing user? Enter 1 if YES, 2 for NO.")
            break
login()

output:

Welcome to David's and John's Tenant Management System!
(1)Admin (2)Customer (3)Exit
Please enter a number: 2
Please enter a number: 2

it keeps on repeating "Please enter a number" twice, instead of once. Sorry if I’m asking a dumb question.

Asked By: Monchi Yui

||

Answers:

you didn’t show your function selection() but looks like you can fix your code next way:

while True:
    selected = selection()
    if selected == "1":
        print("f")  # admin login
    elif selected == "2":
        print("Are you an existing user? Enter 1 if YES, 2 for NO.")
        break
Answered By: Brown Bear
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.