Python Forum
How to make input come after input if certain line inserted and if not runs OtherCode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make input come after input if certain line inserted and if not runs OtherCode
#1
How can I make an input come after another input if a certain piece of text is inputed and if not it prints something else? My current code:
username = input("username>")
if username == "adrian":
    password = input("password>")
    if password == "password1":
        print("login sucsessfull")
         
    else:
        print("password incorrect.")
else:
    print("username incorrect")
But all I get is: "Process finished with exit code 0" and nothing else.
Reply
#2
Works fine when I run it. Your problem must be elsewhere.
Reply
#3
You could loop it.

while True:
    user = input('Username> ')
    if user == 'Myuser':
        passwd = input('Password> ')
        if passwd == 'mypass':
            print(f'{user}, you are now loggerd in')
            break
        else:
            print('Wrong password. Please try again.')
            continue
    else:
        print('Wrong username. Pleasew try again.')
        continue
Adrian_L likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#4
Thanks. But I was running a blank! The code still works. Thanks anyway.
Reply
#5
How do I add another item? So there is Adrian, how do I add another item such as mad?
Reply
#6
You should not test for Adrian or mad or any other user name. You should take the name and then check if the name is valid. In this example I have a dictionary mapping user names to passwords.
users = {"Adrian":"Rocky", "mad":"world"}

def login():
    name = input("Username: ")
    password = input("Password: ")
    if users.get(name, None) == password:
        return name
    return None

if name := login():
    print(f"Welcome {name}")
else:
    print("Invalid username and/or password")
The idea is to avoid writing code for a particular case and instead write code that is generic. This code doesn't care who the users are or what the passwords are. All it cares about is that someone entered a username/password pair that matched an entry in the user database.
Reply
#7
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interactive process for input and output maiya 1 451 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 4 901 Mar-25-2025, 02:40 PM
Last Post: deanhystad
  How to revert back to a previous line from user input Sharkenn64u 2 795 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,088 Nov-24-2024, 12:58 PM
Last Post: deanhystad
Question [SOLVED] Same input different output antarling 2 840 Oct-25-2024, 11:28 PM
Last Post: antarling
  I think I need to delete input data because returning to start fails thelad 2 1,047 Sep-24-2024, 10:12 AM
Last Post: thelad
  Input function oldschool 1 673 Sep-14-2024, 01:02 PM
Last Post: deanhystad
  User input with while loops chizzy101010 2 4,657 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  ValueError: could not broadcast input array from shape makingwithheld 1 2,128 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  email address input jacksfrustration 5 2,206 Jun-26-2024, 08:35 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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