Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help with for-loop printing. I want it to print only once.
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)
# 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!")
Yoriz wrote Jun-25-2020, 04:22 PM:
Please post all code, output and errors (in its entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
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.
Note: Don't use 2 if statements - you change the second one to an elif statement
"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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  My Loop for printing text file goes to infinity Nomex 7 419 Feb-10-2020, 03:13 PM
Last Post: buran
  Print triangle using while loop tuxandrew 3 639 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 626 Feb-19-2019, 02:35 PM
Last Post: marienbad

Forum Jump:

Users browsing this thread: 1 Guest(s)