Home Practicum 1 Practicum 2 Practicum 3 Practicum 4

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!

Conditional statements

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 True. What about the last line in the code above, will it execute if the user is 19? How about if they are 16?

Excercise 1

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 elif.

  • 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.

Test cases:

  1. Year: 2000. Output: You can vote!
  2. Year: 2003. Output: You will have to wait to vote!
  3. Year: 2001, month: 8. Output: You can vote!
  4. Year: 2001, month: 10. Output: You will have to wait to vote!
  5. Year: 2001, month: 9, day: 18. You can vote!
  6. 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.

Excercise 2

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.

  1. 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)
  2. Before asking the user for their age, ask them which country they are from.
  3. Our voting age changes during execution, so it will no longer be spelled with capital letters. Rename VOTING_AGE to voting_age.
  4. Implement an if and else flow 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?
  5. Verify if the user is eligible to vote.

Test cases:

  1. Cuba, 2002. Output: You can vote!
  2. CUBA, 2002, Output: You can vote!
  3. USA, 2002, Output: You will have to wait to vote!
  4. South Korea, 2000, 9, 19 , Output: You will have to wait to vote!

Iterating over lists with a for loop

Before continuing with the exercise, save your code so far to a file and create a new file. Remember the .py extension!

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 years list. 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.

Exercise 3

This exercise will be in a separate .py file!

Let's inform the user about the voting age in any country they are interested in.

  1. Ask the user to provide a list of countries separated by commas.
  2. Turn the provided string into a list of strings using the .split(',') function.
  3. Create a for loop that will do something for each of the provided countries.
  4. Within the for loop paste the if else flow for determining the voting age based on the country name from the previous exercise.
  5. Remember about correct indentation, all the pasted code has to be indented with respect to the for loop!
  6. Extend the code so that it prints the name of each country and its voting age.

Test cases:

  1. Austria. Output:
    Legal voting age in Austria is 16.
  2. 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.