Before session 9
Lists
Watch the videos:
Slides in English
Textbook in English
Test
Go to Moodle and solve the quiz on the video lectures.
Examples
Example 1. List of shoe sizes
The following program reads the foot lengths from the file footlengths.txt, calculates shoe sizes for all numeric foot lengths, and prints shoe sizes, adding the results in a list. After the file is read, the program prompts the user for shoe size, counts how many people in the list have the same shoe size as the size entered and prints the result.
ffile = open("footlengths.txt") shoesizes = [] for foot in ffile: try: f = float(foot) except: print("Invalid input: ", foot.strip()) else: size = int(round(3/2 * (f + 1.5))) print("Foot length:", f, "- Suitable shoe size is", size) shoesizes.append(size) while True: try: size = int(input("Please enter shoe size: ")) except: print("Please enter a number") else: number = shoesizes.count(size) print(number, "people have the same shoe size") break
Example 2. Days of the week
The next program repeatedly asks for the number of the day of the week and prints the name of the day until the user enters 'done' instead of a number.
def day_of_week(n): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] return days[n-1] while True: line = input("Enter number of day of the week or word 'done': ") if line == 'done': break try: number = int(line) except: print("Please enter a number") else: if number < 1 or number > 7: print("The number of the day of the week must be in the range 1-7") else: print("The name of this day of the week is", day_of_week(number))
Exercises
1. Number of boys and girls
Write a function boys_and_girls that has one argument: a list of strings where each element is the first name (which may consist of several names) and gender (B for boys, G for girls) of a person, separated by a space. The function should return a two-element list, where the first and second elements denote the numbers of boys and girls in the list, respectively.
Example
>>> boys_and_girls(['Mati B', 'Kati G', 'Siim Aleksander B', 'Ralf B', 'Veronika G']) [3, 2]
2. Bingo!
Write a function bingo that has no arguments, and which generates bingo numbers: the function should return a list consisting of five different numbers, three of which are in the interval from 1 to 10 and the other two are in the interval from 11 to 20. The list returned must not contain the numbers 1, 2, and 3 at the same time; in this case the function should generate new numbers.
Hint. To generate non-repeating random numbers you can use the function sample from the module random (be careful: the function range doesn't include the endpoint of the interval):
>>> from random import sample >>> sample(range(1, 5), 2) [4, 1]
Example of function output:
>>> bingo() [8, 3, 5, 16, 11]
3. Selecting the trip
A travel agency has written all its trips in the file trips.txt, where every line contains the name of the trip and its price in euros, separated by a semicolon. For example, the content of the file trips.txt may be:
Day cruise to Helsinki;29 Hike in the mountains of Tokyo;700 Romantic weekend in Pärnu;150 Prague pub crawl;400
First create a function listTrips, which takes filename and budget size as its arguments. The function should find all trips in the file that fit into the budget, and return the list of the names of such trips.
For example, with the sample file above, the function should behave as follows:
>>> listTrips('trips.txt', 200) ['Day cruise to Helsinki', 'Romantic weekend in Pärnu']
Next, write a program that asks the user to enter budget size and, using the function listTrips, selects all trips from the file trips.txt that cost less than the budget size entered.
Example of the output:
Enter trip budget: 200 Suitable trips are: Day cruise to Helsinki Romantic weekend in Pärnu
Submit your solutions
Go to Moodle and submit your solutions of exercises as files home1.py, home2.py, home3.py.