Homework for Week 9
Watch the videos:
Go to Moodle and solve the quiz on the video lectures.
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 the 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.
fh = open("footlengths.txt") shoesizes =  for foot in fh: try: f = float(foot) except: print("Invalid input:", foot.strip()) else: size = int(round(3/2 * f + 2)) 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))
1. Calculation of sales prices
Write a function calculatePrices, which takes as an argument a sequence of prices and markup percentage. All prices and the percentage are floating-point numbers.
The function returns a new sequence of prices of the same length, where the initial prices have been increased by the markup percentage, and after that, 20% VAT is added to all prices.
Example of the function call:
>>> calculatePrices([100.0, 1200.0], 10.0) [132.0, 1584.0]
2. 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 comma. 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 Haapsalu,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 Haapsalu']
Next, write a program that asks the user to enter the 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 Haapsalu
Write a function bingo with no arguments that 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 that 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]
Submit your solutions
Go to Moodle and submit your solutions of exercises as files home1.py, home2.py, home3.py.