Welcome to Practicum 3!
Today we will talk about conditional statements and lists, and we will implement a program that finds out if the user can vote. But before that...
Last week recap
Make sure to read the instructions carefully. Do not skip the instructions to go directly to exercises. Part of the exercise required converting from megabytes to megabits, many of you assumed it's the same thing!
Make sure to cast the user input to the expected data type. Even when the user gives us a number (for example of episodes) python by default treats it as a string of characters. You still need to cast it to a number, for example an integer:
num_epi = int(input('How many episodes would you like to download'))or a floating point number:
gpa = float(input('What is your GPA?'))
Make use of the provided test cases. Last week I provided a test case so you can verify whether your program outputs the correct answer. If it doesn't, something is definitely wrong and you'll want to fix it before submitting!
We use conditional statements to change the behavior of the program depending on the circumstances, for example the data a user inputs. Take a look at the code below that checks if the user is old enough to vote:
# by convention variables that don't change during execution # are named with capital letters CURRENT_YEAR = 2019 VOTING_AGE = 18 year_of_birth = int(input('What year were you born? ')) if CURRENT_YEAR - year_of_birth > VOTING_AGE: # this line will only execute if the user is over VOTING_AGE print('You can vote!') elif CURRENT_YEAR - year_of_birth == VOTING_AGE: # this line will only execute if the user is exactly VOTING_AGE # by year but we are not sure if it's past their actual birthday print('Maybe you can vote?') else: # when will this line execute? print('You will have to wait to vote') # how about this line? When will it execute? print('When will this print?')
In python we use indentation to mark which parts of code belong to which
if statement. All the lines below an
if statement that are indented will only execute if the
if statement comes out as a boolean
What about the last line in the code above, will it execute if the user is 19? How about if they are 16?
The code above doesn't really handle the case of users born in 2001: they might be over 18 depending on which month, or even which day of a particular month they were born.
Extend the code above (you can copy and paste it to a new file) by adding another set of if/else statements within our
Create variables with capital letter names for current month and day (September, 18; use a number for the month).
Remember that your code should only execute if the user is born in 2001.
Make sure that the code only asks for the day of birth if necessary.
Year: 2000. Output:
You can vote!Year: 2003. Output:
You will have to wait to vote!Year: 2001, month: 8. Output:
You can vote!Year: 2001, month: 10. Output:
You will have to wait to vote!Year: 2001, month: 9, day: 18.
You can vote!Year: 2001, month: 9, day: 19.
You will have to wait to vote!
Is an element in the list?
We can use the
in operator to find out whether a given item is on a list, for example:
state = input('What is the two letter code of the state are you in?') if state.lower() in ['ct','ma','me','nh','ri','vt']: print('Welcome to New England!') else: print('You should come visit New England in the fall!')
Notice this part:
state.lower() changes whatever the user inputs to lower case. In our case it's important because our list only has lower case state codes and without
.lower() the user's response of
MA would not be recognized as being in New England.
The program we wrote is very US-centric with its assumption that the voting age is always 18. While it is true for many countries, there are quite some where the voting age is lower or higher. When writing software we should not forget about our users from diverse backgrounds.
Find the list of countries with different voting ages on Wikipedia (there are 10 countries/territories with a minimum voting age of 16, 6 with the minimum age of 17, 1 with 19, 4 with 20, and 8 with 21)
Before asking the user for their age, ask them which country they are from.
Our voting age changes during execution, so it will no longer be spelled with capital letters. Rename
voting_age. Implement an
elseflow to similar to the example with the states to find out what the voting age is in their country.
if country.lower() in ['bahrain', 'cameroon', ... ]: voting_age = 20 elif country.lower() in ['kuwait', ...]: # and so on voting_age = 21 else: # what happens if it's not in any of the exceptional cases?Verify if the user is eligible to vote.
Cuba, 2002. Output:
You can vote!CUBA, 2002, Output:
You can vote!USA, 2002, Output:
You will have to wait to vote!South Korea, 2000, 9, 19 , Output:
You will have to wait to vote!
Iterating over lists with a
Before continuing with the exercise, save your code so far to a file and create a new file. Remember the
During the lecture you learnt that one can use a
for loop to do something with every element in a list, try running this code:
years = [2000, 2002, 1990] for year in years: age = 2019-year print('If you were born in', year, 'you are', age, 'years old')
You will see the message printed as many times as there are elements in the
Every time the message is printed it looks different. This is because every time we go throught the loop the variable
year assumes the next value from the
years list, and, therefore, the age changes.
This exercise will be in a separate
Let's inform the user about the voting age in any country they are interested in.
Ask the user to provide a list of countries separated by commas.
Turn the provided string into a list of strings using the
.split(',')function. Create a
forloop that will do something for each of the provided countries. Within the
forloop paste the
if elseflow for determining the voting age based on the country name from the previous exercise. Remember about correct indentation, all the pasted code has to be indented with respect to the
forloop! Extend the code so that it prints the name of each country and its voting age.
Legal voting age in Austria is 16.USA,South Korea,Singapore. Output:
Legal voting age in USA is 18. Legal voting age in South Korea is 19. Legal voting age in Singapore is 21.