📖 Scenario: You are building a simple user login system for a Raspberry Pi project. The system will check if a username and password match stored data to allow access.
🎯 Goal: Create a program that stores user credentials, sets a username and password to check, verifies if the credentials match, and prints a message showing if login is successful or not.
📋 What You'll Learn
Create a dictionary with usernames as keys and passwords as values
Create variables for input username and password to check
Use an if statement to verify if the username exists and the password matches
Print 'Login successful' if credentials match, otherwise print 'Login failed'
💡 Why This Matters
🌍 Real World
User authentication is essential for controlling access to devices like Raspberry Pi projects, home automation, or personal servers.
💼 Career
Understanding basic authentication helps in roles like system administration, IoT development, and software engineering where security is important.
Progress0 / 4 steps
1
Create user credentials dictionary
Create a dictionary called users with these exact entries: 'alice': 'wonderland', 'bob': 'builder', 'carol': 'sunshine'
Raspberry Pi
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Set username and password to check
Create two variables called username and password and set them to 'bob' and 'builder' respectively
Raspberry Pi
Hint
Assign the exact strings to the variables using the equals sign.
3
Check if username and password match
Write an if statement that checks if username is a key in users and if users[username] equals password
Raspberry Pi
Hint
Use in to check if the username exists and compare the stored password with the input password.
4
Print login result
Write a print statement that prints 'Login successful' if login_success is True, otherwise prints 'Login failed'
Raspberry Pi
Hint
Use an if statement to print the correct message based on the login_success variable.
Practice
(1/5)
1. What is the main purpose of user authentication on a Raspberry Pi?
easy
A. To speed up the Raspberry Pi's processing power
B. To verify the identity of a user before allowing access
C. To install new software automatically
D. To connect to the internet faster
Solution
Step 1: Understand user authentication
User authentication is about checking who you are before access is granted.
Step 2: Identify the correct purpose
Only To verify the identity of a user before allowing access describes verifying identity before access, which matches authentication.
Final Answer:
To verify the identity of a user before allowing access -> Option B
Quick Check:
User authentication = verify identity [OK]
Hint: Authentication means checking who you are [OK]
Common Mistakes:
Confusing authentication with performance or internet speed
Thinking authentication installs software
Mixing authentication with network connection
2. Which of the following is the correct way to check a username and password in Python on Raspberry Pi?
easy
A. if username = 'pi' and password = 'raspberry':
B. if username = 'pi' or password = 'raspberry':
C. if username == 'pi' and password == 'raspberry':
D. if username == 'pi' or password == 'raspberry':
Solution
Step 1: Identify correct comparison operator
In Python, '==' checks equality; '=' assigns values.
Step 2: Check logical operators for authentication
Both username and password must match, so use 'and' not 'or'.
Final Answer:
if username == 'pi' and password == 'raspberry': -> Option C
Quick Check:
Use '==' and 'and' for correct check [OK]
Hint: Use '==' to compare and 'and' to require both true [OK]
Common Mistakes:
Using '=' instead of '==' for comparison
Using 'or' instead of 'and' to check both credentials
Mixing assignment and comparison operators
3. What will be the output of this Python code on Raspberry Pi?