0

Hi I am a uni student and this is the first time I am taking a programming subject. I am struggling to create a simple login program that takes two inputs and checks if there is a successful login combination.

Here is the code Ive got so far... thanks in advance for any help!

#Inputs username and password

#If the inputs match the accounts
    #Outputs "welcome back <username>"

#If the inputs dont match the accounts
    #Output "Wrong username or password"

#Initialising variables
use1 = "alice"
use2 = "bob"
use3 = "carol"

ans1 = "greenfingers"
ans2 = "builder"
ans3 = "danvers"

login1 = input("Username: ")
login2 = input("Password: ")

if (login1 == use1 or use2 or use3):  
    if (login2 == ans1 or ans2 or ans3):
        print("Welcome back {}".format(login1))
        
else: 
    print("Wrong username or password")
Blake Lees
  • 13
  • 2
  • This current structure allows any user to login with any password. Probably not the most ideal solution. ;-) – S3DEV Mar 27 '22 at 11:01

3 Answers3

0

you cannot use or in this way, you need write a condition

if (login1 == use1 or login1 == use2 or login1 == use3):  
    if (login2 == ans1 or login2 == ans2 or login2 == ans3):
        print("Welcome back {}".format(login1))
        
else: 
    print("Wrong username or password")
Pratik Agrawal
  • 405
  • 3
  • 17
0

It will be better to use dictionary to simplify check condition and to get better lookup performance:

user_passwords = {
    'alice': 'greenfingers',
    'bob': 'builder',
    'carol': 'danvers',
}

login = input("Username: ")
password = input("Password: ")

if login in user_passwords and user_passwords[login] == password:
    print(f'Welcome back {login}')

else:
    print("Wrong username or password")

Alex Kosh
  • 2,206
  • 2
  • 19
  • 18
0

for python format string = link
and you can use 'in' instead of 'or'

use1 = "alice"
use2 = "bob"
use3 = "carol"

ans1 = "greenfingers"
ans2 = "builder"
ans3 = "danvers"

login1 = input("Username: ")
login2 = input("Password: ")

if ((login1 in [use1,use2, use3 ]) and (login2 in [ans1,ans2, ans3 ])):  
   
        print(f"Welcome back {login1}")
       
        
else: 
    print("Wrong username or password")