Week 2
Python Basics - Conditional Logic and Input
Spyder setup
Start by creating a new folder somewhere on your computer called "python_classes" and then create another folder inside called "week2" - This is where you'll be placing the python scripts (.py files) which you write today
Open up Spyder and create a new file inside the week2 folder called "hello_world.py"
Add a print statement which outputs hello world to the console, and make sure that it runs by pressing the green arrow in the toolbar and checking for the print output in the console
Login system
This week we're going to create a simple login system by using the input() function, and if statements.
Storing user information
For the time being, we're going to store user information in separate variables. Let's start by adding a user with the following details:
Name: Alex Password: 1234
In order to check the name and password in two separate steps, you'll have to store this information in two separate variables
Checking the username
Using the input() function, ask users to enter their username, then check that the value they entered exactly matches the one in the database (e.g. "Alex") - use print statements to let the user know if they entered an incorrect username
Checking the password
Now use input() again when the user has entered the correct username, but this time ask for the password. If the user succeeds in typing in a correct username-password pair, you should print a secret message.
Extension 1 - Case sensitivity
Using the .lower() function (see extension material), make the username check non-caps sensitive.
Try and use the function only once, by reassigning the input immediately after gathering it
Make sure that your database only contains lowercase usernames - otherwise some people will never be able to login
Extension 2 - Two users
Using the logical and/or operations, add the ability for another user (with a distinct username-password pair) to login
You'll need to make use of nested if-statements to ensure the password matches the username
We'll see a far more elegant way of adding more users to our database in subsequent weeks (using dictionaries!).
Extension 3 - Bad Passwords
Warning
Using the in operation (see extension material), have a go at users who have passwords containing the substrings "1234" or "pass" or "word", but only once they've logged in successfully
Password Change
Instead of just berating the user for their poor choice of password, give them the option to change their password (i.e. override the old one).
Once they've changed their password, print it out to confirm it worked.
Last updated