0
0
Raspberry Piprogramming~10 mins

User authentication basics in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - User authentication basics
Start
User enters username
User enters password
Check if username exists
Yes/No
Check password
The flow shows how a system asks for username and password, checks them, and either grants or rejects access.
Execution Sample
Raspberry Pi
users = {'alice': '1234', 'bob': 'abcd'}
username = input('Username: ')
password = input('Password: ')
if username in users and users[username] == password:
    print('Access granted')
else:
    print('Access denied')
This code asks for username and password, then checks if they match stored data to allow or deny access.
Execution Table
StepActionInput/CheckResultOutput
1Ask for usernameUser types 'alice'username = 'alice'
2Ask for passwordUser types '1234'password = '1234'
3Check if username in users'alice' in usersTrue
4Check if password matchesusers['alice'] == '1234'True
5DecisionBoth checks TrueAccess grantedAccess granted
6EndNo more steps
💡 Access granted because username exists and password matches
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
username'''alice''alice''alice''alice''alice'
password'''''1234''1234''1234''1234'
Key Moments - 2 Insights
Why do we check if the username exists before checking the password?
Because if the username is not in the stored users, checking the password would cause an error or be meaningless. Step 3 in the execution_table shows this check.
What happens if the password does not match the stored password?
The program prints 'Access denied' and does not grant access. This is shown in the else branch after step 4 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'username' after step 2?
A'1234'
B''
C'alice'
D'bob'
💡 Hint
Check the variable_tracker table under 'username' after step 2.
At which step does the program decide to grant access?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Decision' action in the execution_table.
If the user types username 'bob' and password 'wrong', what will the output be?
AAccess granted
BAccess denied
CError
DNo output
💡 Hint
Refer to the key_moments explanation about password mismatch.
Concept Snapshot
User authentication basics:
- Ask user for username and password
- Check if username exists in stored data
- Verify password matches stored password
- Grant access if both checks pass
- Otherwise, deny access
Full Transcript
This example shows how a simple user authentication works on a Raspberry Pi. The program asks the user to enter a username and password. It then checks if the username exists in the stored users dictionary. If it does, it compares the entered password with the stored password. If both match, it prints 'Access granted'. Otherwise, it prints 'Access denied'. This process helps protect access to the system by verifying user identity.