Third Class Recap
Updated: 3 days ago
As promised, here is the PowerPoint with the material we covered today:
Login Exercise Solution
Login Exercise Description:
Write a program that asks for and stores a user's username and password, and later asks the user to input the username and password
First, the program should ask the user to enter the correct username
If the entered username matches the stored username, the program should then ask the user for their password
If the user correctly enters the password, the program should print a confirmation message, otherwise, it should print out an error message
username = input("What is your username? ") password = input("What is your password? ") if input("Please enter your username. ") == username: if input("Please enter your password. ") == password: print("Logged in.") else: print("Password is incorrect.")
Solution #2 (Slightly Different):
username = input("What is your username? ") password = input("What is your password? ") inputted_username = input("Please enter your username. ") if inputted_username == username: inputted_password = input("Please enter your password. ") if inputted_password == password: print("Logged in.") else: print("Password is incorrect.")
A function is a block of code that takes in an input and returns an output
It can be thought of as a machine where you put in some object(s) and get out other object(s)
range(), which we talked about last week, is an example of a built-in function
Python has a number of built-in functions like range(), print(), and input() that have already been defined for us, so we can just call them to use them
Calling a function essentially means that we trigger the function by giving it an input
This is the general format of a function:
def function_name(parameters): do stuff here return outputs #here's how you would call the function and print the output(s) print(function_name(parameters))
def = define
parameters = inputs
Note: If you don't specify a return statement at the end of a function, your function will return nothing by default
Why are functions useful? Just like loops, they reduce the amount of work that programmers do, making coding easier, cleaner, and more efficient.
Now, let's look at an example of a function:
def login(username): #function: login, input(s): string, output: bool if username == "mana": return True else: return False username = input("Enter username: ") login_result = login(username) if login_result == True: print("Success!") else: username = input("Re-enter username: ") login(username)
In this case, the program will only check the True/False status of the username and act on it once. If we want the program to keep prompting the user to re-enter their username, we would use a while loop:
def login(username): if username == "mana": return True else: return False username = input("Enter username: ") login_result = login(username) while login_result == False: username = input("Re-enter username: ") login_result = login(username) print("Success!")
TRY1: Write a function that takes a name as an input and prints out a greeting that includes the name (you can do this using string concatenation).
TRY2: Write a function called add_one takes a number as input and returns the number + 1
TRY3: Code your own mini-calculator! Write four functions, add, subtract, multiply, and divide, that each take in two numbers and return the result of adding/subtracting/multiplying/dividing.
A list is a data structure that is more or less what it implies -- a collect/list of data
In other programming languages such as Java, a list is known as an array
We use square brackets ('[' and ']') to define a list, and separate the elements or items in our list using commas.
names = ["Rachael", "Mana", "Minseo"]
We can enter any type of data in our list:
example_list = [42, "hello, world!", 3.0, True]
...and we can print a list like this:
Why do we use lists? First of all, lists are useful for grouping data and storing them in a variable. As we will soon learn, it's very easy to access items in a list, add items, change items, and more. Lists are quite flexible and are pretty amazing.
Accessing Items in a List:
To access an item in a list, we use index
Each item in a list has a certain index (a position, basically), that we refer to in order to access the item
In Python, index starts at 0. The first item in a list will have index 0.
To access an element of a list using index, we write: list[index]
names #This should print 'Minseo' example_list #This should print 42
Looping through a List:
Programmers often use for-loops and while loops with lists. You can iterate (go over the range of items in a list and perform the same task) through the list using these loops. Let's take a look at an example:
scores = [96, 93, 84, 90, 98, 81] print("Here are your scores for the term:") for score in scores: print(score)
Note: score is a variable that keeps getting updated as Python iterates through the list. In general, it is good practice to name this variable whatever makes the most sense and is most relevant to the other variable names in your program.
TRY1: Create a list called friends that stores the names of your friends. Loop through the list and print a greeting for each friend.
TRY2: Create a list within a list, like [2, [3, 4, 5], 6]. Experiment with Python and try to figure out how to access elements within the inner list (try to do this by yourself, without using Google).
As always, if you have any questions, feel free to contact us.
mana = "email@example.com" rachael = "firstname.lastname@example.org" minseo = "email@example.com"