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.
Works fine when I run it. Your problem must be elsewhere.
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
Thanks. But I was running a blank! The code still works. Thanks anyway.
How do I add another item? So there is Adrian, how do I add another item such as mad?
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.