Python Forum
Help with for-loop printing. I want it to print only once.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with for-loop printing. I want it to print only once.
#1
Hi I'm new to python and I'm currently working on a python login programm for school. this is what I have so far:
The thing is when I intentionally type in a password or username wrong, I want the programm to type out username or password invalid. However it prints it out twice or thrice. This is because of the for loop that is going through my list and checking if the object's name or password are correct. If it's not it'll print out invalid etc. It does this twice or thrice. My question is: How can I make my programm type username or password invalid once? (btw I'm new to python, so go easy on me)
Thanks!
# making a login system.

#ask account prompt/function.
ask_account = input("Do you have an account? ")

#class to create new users
class users:
    def __init__(self, username, password):
        self.username = username
        self.password = password

# This is the user list.
user_list = [
users("Se", "ben0"),
users("Soar", "ben7")
]

# continuing function to access your account
if ask_account == "yes":
    username_ = input("What is your username: ")
    password = input("What is your password: ")
    for element in user_list:
        if username_ == element.username and password == element.password:
            print("Welcome {}".format(element.username) +"." + "You are logged in!")
        if username_ != element.username or password != element.password:
            print("Invalid username or password!")

# continuing function to create a new account and access your new account
if ask_account == "no":
    user_list.append(users(input("insert username: "), input("insert password ")))
    print("You have created a new account!")
    username_ = input("What is your username: ")
    password = input("What is your password: ")
    for x in user_list:
        if username_ == x.username and password == x.password:
            print("Welcome {}".format(x.username) +"." + "You are logged in!")
        if username_ != x.username or password != x.password:
            print("Invalid username or password!")
Reply
#2
You can use break to exit your for loops once a condition you are testing for is satisfied. You can read about the break statement in the Python documentation here.
Reply
#3
Note: Don't use 2 if statements - you change the second one to an elif statement
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing During a Loop apeltes 16 5,034 Oct-21-2021, 12:19 AM
Last Post: apeltes
  My Loop for printing text file goes to infinity Nomex 7 4,149 Feb-10-2020, 03:13 PM
Last Post: buran
  Print triangle using while loop tuxandrew 3 4,840 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Program that, inside a loop, does multiple things. needs to print in a certain way reidmcleod 1 2,639 Feb-19-2019, 02:35 PM
Last Post: marienbad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020