Building a password checker
You've seen how conditional statements can be used to check for equality. Now you have the skills to build a custom function, you'll combine these two techniques to build a function called password_checker
that compares a user's password to a submission, conditionally printing an output depending on the results.
This is a part of the course
“Intermediate Python for Developers”
Exercise instructions
- Define the
password_checker
function, which should accept one argument calledsubmission
. - Check if
password
is equal tosubmission
. - Add a keyword enabling the conditional printing of
"Incorrect password"
ifpassword
andsubmission
are different. - Call the function, passing
"NOT_VERY_SECURE_2023"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
password = "not_very_secure_2023"
# Define the password_checker function
____
# Check that the password variable and the submission match
if ____ ____ ____:
print("Successful login!")
# Otherwise, print "Incorrect password"
____:
print("Incorrect password")
# Call the function
____("____")
This exercise is part of the course
Intermediate Python for Developers
Dive into the Python ecosystem, discovering modules and packages along with how to write custom functions!
Learn the fundamentals of functions, from Python's built-in functions to creating your own from scratch!
Exercise 1: Defining a custom functionExercise 2: Custom function syntaxExercise 3: Cleaning text dataExercise 4: Building a password checkerExercise 5: Default and keyword argumentsExercise 6: Positional versus keyword argumentsExercise 7: Adding a keyword argumentExercise 8: Data structure converter functionExercise 9: DocstringsExercise 10: Single-line docstringsExercise 11: Multi-line docstringsExercise 12: Arbitrary argumentsExercise 13: Adding arbitrary argumentsExercise 14: Arbitrary keyword argumentsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.