Skip to content

script_6.py - Login Validator

Code

Python
#!/usr/bin/env python3
"""Login validator"""

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

if username == "admin" and password == "secret123":
    print("Access granted")
else:
    print("Access denied")

Explanation

Line 4: username = input("Username: ") - input() returns string type (str) - username stores the user's input as a string - No type conversion - strings can contain any characters

Line 5: password = input("Password: ") - Another input() call for password - Note: input() shows typed characters (not secure for real passwords) - password variable is also string type

Line 7: if username == "admin" and password == "secret123": - == checks string equality (case-sensitive) - and is a logical operator that requires BOTH conditions to be True - Returns True only if username matches "admin" AND password matches "secret123" - If either condition is False, the entire expression is False

Line 8: print("Access granted") - Only executes when both username and password match

Line 9: else: - Executes when ANY part of the condition fails - Handles: wrong username, wrong password, or both wrong