Advertisement
Oranssialeksi

Kierros 8 teht 1

Nov 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import datetime
  2.        
  3. def convert_date_text_to_datetime_object(date_as_text):
  4.     "Returns a datetime.date object converted from string 'date_as_text'."
  5.     date_as_list = date_as_text.split(".")
  6.     day, month, year = [int(part) for part in date_as_list]
  7.     return datetime.date(year, month, day)
  8. def print_file_contents(name_of_the_file):
  9.     "Prints the contents of the file with the name 'name_of_the_file'. Make sure the file is not still open elsewhere before calling this function."
  10.     file = open(name_of_the_file, 'r')
  11.     line = file.readline()
  12.     while line != '':
  13.         print(line, end='')
  14.         line = file.readline()
  15.     file.close()
  16. def main():
  17.     print("Enter the name of the file to be created for your screen time data:")
  18.     filename = input()
  19.     file = open(filename, 'w')
  20.     print("Enter the start date in format 'DD.MM.YYYY':")
  21.     date_text = input()
  22.     date1 = convert_date_text_to_datetime_object(date_text)
  23.    
  24.     # Write your code here!
  25.     print("Enter your screen watching time for each day (in minutes) in the format '[Phone minutes] [PC minutes] [TV minutes] [other minutes]'")
  26.     Maarat_summa = 0
  27.     temp = 0
  28.     while True:
  29.         Maara = input("Enter your screen time on {}:".format(date1))
  30.         if Maara == "":
  31.             break
  32.         Maarat = Maara.split(" ")
  33.         date2 = str(date1)
  34.         rivi = date2 + ": " + "/".join(Maarat)
  35.         file.write(rivi + "\n")
  36.         j = 1
  37.         for i in range(0, len(Maarat)):
  38.             Maarat[i] = float(Maarat[i])
  39.         Maarat_summa = Maarat_summa + sum(Maarat)
  40.         temp += 1
  41.         date1 = date1 + datetime.timedelta(days=1)
  42.     # (You can write other functions if you find it of use)
  43.     print("-" * 100)
  44.     print("Screen times saves succesfully in the file '{}'".format(filename))
  45.     print("Saved the screen times of", temp, "days.")
  46.     if temp != 0:
  47.         print("Total screen time from this period is",round(Maarat_summa/60, 1) ,"hours and daily average is",round((Maarat_summa/60)/temp, 1),"hours.")
  48.     print("Let's look inside the file. It looks as follows:")
  49.     print("-" * 100)
  50.     file.close()
  51.     print_file_contents(filename)
  52.    
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement