-3

I am tinkering with some code online and stumbled upon this one :

import os
import time
#Must Access this to continue.
def main():
    while True:
        UserName = input ("Enter Username: ")
        PassWord = input ("Enter Password: ")

        if UserName == 'Bob' and PassWord == 'rainbow123':
            time.sleep(1)
            print ("Login successful!")
            logged()

        else:
            print ("Password did not match!")

def logged():
    time.sleep(1)
    print ("Welcome to ----")

main()

I am trying to make it so after the username and password has been implemented, that this sequence happens next:

def mainname():
    while True:
        Firstname = input("Enter First Name:")
        Lastname = input("Enter Last Name:")

        if Firstname == 'Cher, Madonna':
            time.sleep(2)
            print("May I have your autograph, please?")

        else:
            print("that's a nice name")
mainname()

But what happens is, after I put in the username and password it then loops back to inputting the username and password. How do I make it so after the username and password has been implemented, then it goes to the first name and last name sequence? Thank you very much for the help

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
kwikmatt
  • 11
  • 2
  • 3
    It loops back because there is an endless `while` loop. If you don't need it to repeat, maybe delete the loop? – Lev Levitsky Nov 04 '20 at 15:53
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Nov 04 '20 at 15:57

2 Answers2

0

The code that checks the password is in a while True: loop, so it will keep looping and asking for the password regardless.

You want to break out of the loop on success, so add a break after you successfully log in.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Also that `if Firstname == 'Cher, Madonna':` line doesn't do what I think you want it to do. – Blindy Nov 04 '20 at 15:54
  • Because it wasn't part of the question. I'm just *commenting* on unrelated things, so I use comments. – Blindy Nov 04 '20 at 15:57
0

Right after you have confirmed that the password is correct just add break. This will end the while loop as the correct username and password is entered.