0
0
Raspberry Piprogramming~10 mins

User authentication basics in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the entered password matches the stored password.

Raspberry Pi
if entered_password [1] stored_password:
    print("Access granted")
else:
    print("Access denied")
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison '=='.
2fill in blank
medium

Complete the code to ask the user for their username.

Raspberry Pi
username = input([1])
Drag options to blanks, or click blank then click option'
A"Enter your username: "
B'Enter your email: '
C'Enter your password: '
D"Enter your PIN: "
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong prompt message that asks for password or PIN.
3fill in blank
hard

Fix the error in the code to securely check the password using hashing.

Raspberry Pi
import hashlib

hashed_input = hashlib.sha256(entered_password.encode()).hexdigest()
if hashed_input [1] stored_hashed_password:
    print("Access granted")
else:
    print("Access denied")
Drag options to blanks, or click blank then click option'
A=
B==
Cis
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which assigns instead of compares.
Using 'is' which checks identity, not equality.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores usernames and their hashed passwords only if the password length is greater than 5.

Raspberry Pi
user_hashes = {user: hashlib.sha256([1].encode()).hexdigest() for user, [2] in users.items() if len(password) > 5}
Drag options to blanks, or click blank then click option'
Apassword
Bpassw
Cpwd
Dpasswd
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names that are not defined in the loop.
5fill in blank
hard

Fill all three blanks to create a function that checks if a username exists and the password matches the stored hash.

Raspberry Pi
def authenticate([1], [2]):
    if [1] in user_db and hashlib.sha256([2].encode()).hexdigest() == user_db[[1]]:
        return True
    return False
Drag options to blanks, or click blank then click option'
Ausername
Bpassword
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names or using undefined variables.