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


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
  Help with to check an Input list data with a data read from an external source sacharyya 3 390 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 1,007 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  pyaudio seems to randomly halt input. elpidiovaldez5 2 358 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Receive Input on Same Line? johnywhy 8 695 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  manually input data jpatierno 0 336 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Using string input for boolean tronic72 3 682 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using input command akbarza 4 1,091 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 640 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Waiting for input from serial port, then move on KenHorse 2 930 Oct-17-2023, 01:14 AM
Last Post: KenHorse
  Input network device connection info from data file edroche3rd 6 991 Oct-12-2023, 02:18 AM
Last Post: edroche3rd

Forum Jump:

User Panel Messages

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