Home > database >  How do I correctly format putting an "if" in python
How do I correctly format putting an "if" in python

Time:05-25

How would I put "if" or "else" in code like this?

user = input('Login: Username\n')
time.sleep(1)
password = input('Login: Password\n')
print('Welcome, %s.' % user)

CodePudding user response:

I think this is what you want to do. But this is not a secure way of doing password requests.

user = input('Login: Username\n')
password = input('Login: Password\n')

users = {"You" : "1234", "Me" : "abcd"}

if user in users.keys():
    if users[user] == password:
        print("Welcome, {u}".format(u=user))
    else:
        print("Wrong password")
    
else:
    print("This user does not exist")

CodePudding user response:

Here is a simple version on how this should work but is not secure at all, but functions (almost) the same way as you requested (came up with this under 5 mins)

user = input('Login Username: ')
password = input('Login Password: ')
  if user == ("Your username here"):
    if password == ("Your password here"):
      print("Welcome ", user)

Also you do not need "time.sleep()" as it only slows down your code

  • Related