Created: 2025-05-06 di 14:17
The following piece of code has 2 main (syntax) errors. Explain below what these errors are and how you would fix them. Do not rewrite the fix code!
Write a function that takes the list of dictionaries and a rating as arguments. It should return a new list with the names of the movies that have a rating higher than the one given as an argument.
movie_db = [
{"title": "A Boy and His Dog", "year": 1975, "rating": 6.6},
{"title": "Ran", "year": 1975, "rating": 8.3},
{"title": "True Grit", "year": 2010, "rating": 8.0},
{"title": "Scanners", "year": 1981, "rating": 6.7},
{"title": "Barbie", "year": 2023, "rating": 6.8},
{"title": "Anora", "year": 2024, "rating": 8.3},
{"title": "Caddyshack", "year": 1980, "rating": 7.2},
{"title": "Gone With the Wind", "year": 1939, "rating": 8.2}
]
def rating_filter(l, rating):
lst = []
for i in l:
if i["rating"] > rating:
lst.append(i["title"])
return lst
print(rating_filter(movie_db,7))
Write a function that takes the list of dictionaries and a year as arguments. It should return a list with the names of the movies that were released before that year.
movie_db = [
{"title": "A Boy and His Dog", "year": 1975, "rating": 6.6},
{"title": "Ran", "year": 1975, "rating": 8.3},
{"title": "True Grit", "year": 2010, "rating": 8.0},
{"title": "Scanners", "year": 1981, "rating": 6.7},
{"title": "Barbie", "year": 2023, "rating": 6.8},
{"title": "Anora", "year": 2024, "rating": 8.3},
{"title": "Caddyshack", "year": 1980, "rating": 7.2},
{"title": "Gone With the Wind", "year": 1939, "rating": 8.2}
]
def year_filter(l, year):
lst = []
for i in l:
if i["year"] < year:
lst.append(i["title"])
return lst
print(year_filter(movie_db,1990))
Write a function that consumes the list of movies and a character. It should count how many movies contain the given character in their names (case insensitive!)
movie_db = [
{"title": "A Boy and His Dog", "year": 1975, "rating": 6.6},
{"title": "Ran", "year": 1975, "rating": 8.3},
{"title": "True Grit", "year": 2010, "rating": 8.0},
{"title": "Scanners", "year": 1981, "rating": 6.7},
{"title": "Barbie", "year": 2023, "rating": 6.8},
{"title": "Anora", "year": 2024, "rating": 8.3},
{"title": "Caddyshack", "year": 1980, "rating": 7.2},
{"title": "Gone With the Wind", "year": 1939, "rating": 8.2}
]
def count_chars(l, char):
count = 0
for i in l:
if char.upper() in i["title"].upper():
count += 1
return count
print(count_chars(movie_db,"T"))
Open, read and save the information of the file in a Data Type of your choice. Write this as a function that consumes the name of the file. Briefly justify the data type that you have chosen.
file_path = "/home/misha/Downloads/netflixfeesubscriprion.txt"
def read_data(file_name):
with open(file_name, "r") as f:
# # option manual
# lst = f.readlines()[8:]
# option auto
lines = f.readlines()
lst = []
for i in lines:
if "#" in i:
continue
else:
lst.append(i)
dict_list = []
for i in lst:
dict1 = {}
split = i.strip("\n").split(",")
dict1["country"] = split[0]
dict1["basic"] = split[1]
dict1["standard"] = split[2]
dict1["premium"] = split[3]
dict_list.append(dict1)
return dict_list
print(read_data(file_path))
Which country (or countries) have the cheapest premium subscription?
file_path = "/home/misha/Downloads/netflixfeesubscriprion.txt"
def read_data(file_name):
with open(file_name, "r") as f:
# # option manual
# lst = f.readlines()[8:]
# option auto
lines = f.readlines()
lst = []
for i in lines:
if "#" in i:
continue
else:
lst.append(i)
dict_list = []
for i in lst:
dict1 = {}
split = i.strip("\n").split(",")
dict1["country"] = split[0]
dict1["basic"] = split[1]
dict1["standard"] = split[2]
dict1["premium"] = split[3]
dict_list.append(dict1)
return dict_list
dict = read_data(file_path)
# We have to start somewhere, so we
# can pick the first item of the list.
cheapest_country = dict[0]["country"]
cheapest = dict[0]["premium"]
# Find the cheapest
for i in dict:
if i["premium"] < cheapest:
cheapest = i["premium"]
cheapest_country = i["country"]
print(cheapest_country, cheapest)
# Check if this cheapest price happens
# in multiple countries
for i in dict:
if i["premium"] == cheapest:
print(i["country"])
What is the average price for each subscription across all nations? Print your answer as floating numbers rounded to 1 decimal
file_path = "/home/misha/Downloads/netflixfeesubscriprion.txt"
def read_data(file_name):
with open(file_name, "r") as f:
# # option manual
# lst = f.readlines()[8:]
# option auto
lines = f.readlines()
lst = []
for i in lines:
if "#" in i:
continue
else:
lst.append(i)
dict_list = []
for i in lst:
dict1 = {}
split = i.strip("\n").split(",")
dict1["country"] = split[0]
dict1["basic"] = float(split[1])
dict1["standard"] = float(split[2])
dict1["premium"] = float(split[3])
dict_list.append(dict1)
return dict_list
dict = read_data(file_path)
sum_basic = 0
sum_standard = 0
sum_premium = 0
count = 0
for i in dict:
sum_basic += i["basic"]
sum_standard += i["standard"]
sum_premium += i["premium"]
count += 1
for i in [sum_basic, sum_standard, sum_premium]:
answer = i/count
print(f"{answer:.1f}")
Which countries have the largest difference between the premium and basic subscriptions?
file_path = "/home/misha/Downloads/netflixfeesubscriprion.txt"
def read_data(file_name):
with open(file_name, "r") as f:
# # option manual
# lst = f.readlines()[8:]
# option auto
lines = f.readlines()
lst = []
for i in lines:
if "#" in i:
continue
else:
lst.append(i)
dict_list = []
for i in lst:
dict1 = {}
split = i.strip("\n").split(",")
dict1["country"] = split[0]
dict1["basic"] = float(split[1])
dict1["standard"] = float(split[2])
dict1["premium"] = float(split[3])
dict_list.append(dict1)
return dict_list
dict = read_data(file_path)
# We have to start somewhere, so we
# can pick the first item of the list.
biggest_diff = dict[0]["premium"]-dict[0]["basic"]
biggest_diff_country = dict[0]["country"]
# Find the cheapest
for i in dict:
diff = i["premium"] - i["basic"]
if diff > biggest_diff:
biggest_diff = diff
biggest_diff_country = i["country"]
print(biggest_diff_country, biggest_diff)
print("-----------")
# Check if this max difference happens
# in multiple countries
for i in dict:
diff = i["premium"] - i["basic"]
if diff == biggest_diff:
print(i["country"])
For a country of your choice, report the difference (in dollars) between: 1) the basic and standard prices, 2) the standard and the premium prices, and 3) and the basic and the premium prices. Report these three as floating numbers rounded to 1 decimal.
import random
file_path = "/home/misha/Downloads/netflixfeesubscriprion.txt"
def read_data(file_name):
with open(file_name, "r") as f:
# # option manual
# lst = f.readlines()[8:]
# option auto
lines = f.readlines()
lst = []
for i in lines:
if "#" in i:
continue
else:
lst.append(i)
dict_list = []
for i in lst:
dict1 = {}
split = i.strip("\n").split(",")
dict1["country"] = split[0]
dict1["basic"] = float(split[1])
dict1["standard"] = float(split[2])
dict1["premium"] = float(split[3])
dict_list.append(dict1)
return dict_list
list_dict = read_data(file_path)
# Pick random country
i = random.randint(0,len(list_dict))
dict = list_dict[i]
diff1 = dict["premium"]-dict["basic"]
diff2 = dict["premium"]-dict["standard"]
diff3 = dict["standard"]-dict["basic"]
print(dict["country"])
for i in [diff1, diff2, diff3]:
print(f"{i:.1f}")