CSC 127A Lecture 18: CSC 127A/110 Week 7 10.3.16
Week 7 10/3/16
Hours question:
Would like each person in their file to have their ID number, name and the hours that they worked. We
want to find out how many hours and an average for each day.
• Use split function to divide lines with more than one type of data
1. def main():
2. file = open("hours.txt")
3. lines = file.readlines()
4.
5. for line in lines:
6. parts = line.split()
7. print(parts[1] + " (ID#" + parts[0] + ") worked ", end='')
8.
9. sum = 0
10. for i in range(2, len(parts)):
11. sum += float(parts[i])
12.
13. days = (sum / (len(parts) - 2) # to reduce long print lines
14. print(str(round_to(sum, 1)) + " hours (" + str(round_to(days, 2)) + " hours/day)"
15.
16. '''''
17. def round1(number):
18. # 34.23425 -> 34.2
19. return round(number * 10) / 10
20.
21. def round2(number):
22. # 34.23425 -> 34.23
23. return round(number * 100) / 100
24. '''
25.
26. def round_to(number, digits):
find more resources at oneclass.com
find more resources at oneclass.com