Professional Documents
Culture Documents
100
QUID Section
1|Page
Question 1:
Write a loop that calculates the total of the following series of numbers:
2 4 6 100
+ + + ⋯ +
1 3 5 99
total = 0
for i in range(1,101):
if i%2==0:
j = i-1
total+=(j/i)
print("Total=",total)
22 marks
Question 2:
Write a function number_split that receives a list of numbers and returns two
lists: one includes only float numbers (without zero) and the other one includes
only integer numbers.
• Test the number_split function in a main program using the following list:
[0.3,4,0,0.5,22,0.1].
• Display the output (i.e., print the list of float and integer numbers) in your
main program.
def number_split(numbers):
floats = []
integers = []
for i in numbers:
if 0 < i < 1:
floats.append(i)
else:
integers.append(i)
return floats, integers
def main():
2|Page
numbers = [4,0.6,0,23,0.2,0.1]
f,i = number_split(numbers)
print("Floats:",f)
print("Integers:",i)
main()
26 marks
Question 3:
26 marks
3|Page
Question 4:
The file TV.txt contains data about movies; every movie has title, year, and rate.
Each of this data is on separate line as follows:
title(string)\nyear(integer)\nrating(float)
def process_list(file_name):
inFile = open(file_name,'r',encoding='utf-8')
title = inFile.readline().strip()
ratings = []
ratings2020 = []
maxRating = 0
maxTitle = ''
while title != '':
year = int(inFile.readline().strip())
rating = float(inFile.readline().strip())
if year == 2020:
ratings2020.append(rating)
ratings.append(rating)
if rating > maxRating:
maxRating = rating
maxTitle = title
title = inFile.readline().strip()
inFile.close()
print("Average rating of all TV shows:", sum(ratings)/len(ratings))
print("Average ratings of TV shows from 2020:",sum(ratings2020)/len(ratings2020))
print("Title of TV show with the highest rating:",maxTitle)
process_list("TV.txt")
26 marks
Don’t forget to upload your zip file on blackboard and submit your exam paper
4|Page