Ask Question
2 May, 06:55

Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input file will contain temperature readings for each day of the week. Your function should read each line from the given filename, parse and process the data, and return the required information. Your function should return the highest temperature for the whole week. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return - 1. Remember temperature readings can be decimal and negative numbers.

+3
Answers (1)
  1. 2 May, 07:50
    0
    The solution is written in Python 3

    def maxTemp (fileName) : try: with open (fileName) as file: raw_data = file. readlines () highest = 0 for row in raw_dа ta: if (row = = "/n") : continue data = float (row) if (highest < data) : highest = data return highest except FileNotFoundError as found_error: return - 1 higestTemp = maxTemp ("text1. txt") print (higestTemp)

    Explanation:

    Firstly, create a function maxTemp that take one input file name (Line 1). Next create an exception handling try and except block to handle the situation when the read file is not found or cannot be open, it shall return - 1 (Line 2, Line 15 - 16).

    Next use the readlines method to read the temperature data from the input file (Line 4). Before we traverse the read data using for loop, we create a variable highest to hold the value of max temperature (Line 5). Let's set it as zero in the first place.

    Then use for loop to read the temp data line by line (Line 6) and if there is any empty line (Line 7) just ignore and proceed to the next iteration (Line 7 - 8). Otherwise, the current row reading will be parsed to float type (Line 10) and the parsed data will be check with the current highest value. If current highest value is bigger than the current parsed data (), the program will set the parse data to highest variable (Line 11 - 12).

    After finishing the loop, return the highest as output (Line 14).
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers