Before session 7
Lists
Watch the video:
Slides in English and in Russian
Textbook in English
Helpful link in Russian_7.pdf
Textbook in Estonian (Järjendid, for-tsükkel, Järjendite töötlemine, Funktsioon range, Veel järjendioperatsioone)
Test
Go to Moodle and take the seventh test on the video lectures.
Exercises
NB! Make sure that the text files (.txt) are in the same folder as your Python code. Otherwise Python will not find the file.
Example 1. Shoe size
The following program reads the foot lengths from the file foot.txt, calculates a suitable shoe size for each foot length and prints the shoe size out. Moreover, the program puts the suitable shoe size into the list. Once the file is read, the program prompts the user for the shoe size. The program then counts how many people in the list have the same shoe size as the entered size and prints the result out.
ffile = open("foot.txt") shoes = [] for foot in ffile: try: f = float(foot) shoe = int(round(3/2 *(f + 1.5))) print("Foot length:", f, "Suitable shoe size is", shoe) shoes.append(shoe) except: print("Invalid input") while True: try: size = int(input("Please enter the size: ")) number = shoes.count(size) print(number, "people have the same shoe size") break except: print("Please enter a number")
Example 2. Days of the week
The next program repeatedly asks the number of a day of the week and prints out the name of the day until the user enters "done" instead of the 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) if number < 1 or number > 7: print("The number of a 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)) except: print("Please enter a number")
Exercise 1. Celsius and Fahrenheit
Create a text file called temps.txt. In the file, write temperatures in Celsius (each value on a separate line). Create a program which reads temperatures from a file (that contains temperatures in Celcius), converts the temperatures to Fahrenheit and stores the converted temperatures into a list (Hint: you can use the function we wrote in one of the practice sessions to convert temperatures from Celcius to Fahrenheit). The program should print out the average, maximum and minimum of the Fahrenheit temperatures.
Exercise 2. Number of days in months
Rewrite the number of days in months program so that the program prompts the user for a file name and reads the dates from the file. The dates in the file should be written in the format dd.mm.yyyy (see an example). The program has to look for the month in each line (hint: use split) and print out the number of days in that month. Rewrite the function so that it does not contain conditional (if) statements for returning the number of days. The function should do it using a list (look at example 2). Do not add the check for a leap year (let’s say that there are 28 days in February).
Go to Moodle and upload your solutions into Homework 7.