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
#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


Messages In This Thread
RE: How to make input come after input if certain line inserted and if not runs OtherCode - by deanhystad - Apr-02-2021, 04:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Input error correction. shakoun 4 305 Jun-07-2024, 12:59 PM
Last Post: menator01
  Having difficulty with threads and input() sawtooth500 13 639 Jun-07-2024, 08:40 AM
Last Post: Gribouillis
  problem program runs in a loop jasserin 0 207 May-18-2024, 03:07 PM
Last Post: jasserin
  Keep getting "SyntaxError: incomplete input" but can't see why. Pedroski55 3 682 May-14-2024, 07:46 AM
Last Post: gavin34
  Waiting for input from serial port, then move on KenHorse 3 1,465 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  Help with to check an Input list data with a data read from an external source sacharyya 3 618 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 1,291 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  pyaudio seems to randomly halt input. elpidiovaldez5 2 528 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Receive Input on Same Line? johnywhy 8 1,033 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  manually input data jpatierno 0 425 Nov-10-2023, 02:32 AM
Last Post: jpatierno

Forum Jump:

User Panel Messages

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