Question

In: Computer Science

Question Objective: The objective of this lab exercise is to give you practice in programming with...

Question

Objective:

The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to:

  1. Declare list objects
  2. Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component
  3. Iterate through a list looking for specific values using a forin loop repetition construct
  4. Pass an entire list argument to a function
  5. Process a list parameter received by a function

Discussion:     

For this exercise you will be simulating a Weather Station responsible for recording hourly temperatures and reporting on certain characteristics (e.g., average temperature) from your recordings. You will also be expanding on your experience in creating functions and passing them more complex parameters (i.e., complete list arguments – sometimes empty and sometimes full).

Specifications:

DDI&T a Python program to input, store, and process hourly temperatures for each hour of the day (i.e., 24 temperatures). Your program should be divided logically into the following parts:

  1. In function main() declare an empty List container:

         HourlyTemperatures = []    

  1. Pass the empty HourlyTemperatures list to a function,

GetTemperatures(HourlyTemperatures)

         This function must interactively prompt for and input temperatures for each of the 24 hours in a day (0 through 23). For each temperature that is input, verify that its value lies in the range of minus 50 degrees and plus 130 degrees (i.e., validate your input values). If any value is outside the acceptable range, ask the user to re-enter the value until it is within this range before going on to the next temperature (HINT: use a nested loop (e.g., Python’s equivalent to a do-while loop) that exits only when a value in the specified range has been entered). Store each validated temperature in its corresponding element of the list container passed as a parameter to the function (Hint: Look at the ‘append(…)’ function that can be called on a list container).

  1. Next pass the filled list container to a function,

ComputeAverageTemp(HourlyTemperatures)

         This function computes the average temperature of all the temperatures in the list and returns this average to the calling function.

  1. Finally, pass the filled list container and the computed average temperature to another function,

                  DisplayTemperatures(HourlyTemperatures, AverageTemp)

         which displays the values of your temperature list in a columnar format followed by the values for the high temperature, low temperature, and average temperature for the day.  NOTE: If you want to create separate function(s) to find and return the high and low temperature values, then feel free to do so!

The resulting output should look something like this:

Hour                          Temperature

00:00                                42

01:00                                42

. . . . .                                . . . // Your program output must include all of these too!

22:00                                46

23:00                                48

High Temperature:          68

Low Temperature:           42

Average Temperature:    57.4

5.      Since you have several created functions to perform each of the major steps of your solution, your main(): function should be quite simple. The pseudo code for main() might look something like this:

                  main()

                       #declare any necessary variable(s) and HourlyTemperatures list

                       while user-wants-to-process-another-days’-worth-of-temperatures

                           call GetTemperatures(HourlyTemperatures)

                                 call ComputeAverageTemp(HourlyTemperatures)

                                 call DisplayTemperatures(HourlyTemperatures, AverageTemperature)

                                 ask if user want to process another days’ worth of temperatures

6.      Test your program with at least two (2) different sets of temperatures. Make sure you enter values to adequately test your temperature-validation code (e.g., temperatures below –50 and above +130 degrees).

Deliverable(s):

Your deliverable should be a Word document with screenshots showing the sample code you have created, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them.

Turn in the properly documented source listing of your program and complete outputs from at least TWO (2) test “runs”. Additionally, turn in screen captures of some of your interactive inputs demonstrating that your program properly detects invalid inputs and prompts the user to re-enter the temperature(s).

Solutions

Expert Solution

Screenshot

Program

#Create a function to get a days each hours temperature
def GetTemperatures(HourlyTemperatures):
    #Loop to get temperature 0-23
    for i in range(0,24):
        prompt="Enter temperature in "
        if(i<=9):
            prompt+='0'+str(i)+":00 time in degrees: "
        else:
            prompt+=str(i)+":00 time in degrees: "
        temp=int(input(prompt))
        #Validity check
        while(temp<-50 or temp>130):
            print('ERROR!!temperature should be -50 to 130 degrees.Re-enter')
            prompt="Enter temperature in "
            if(i<9):
                prompt+='0'+str(i)+":00 time in degrees: "
            else:
                prompt+=str(i)+":00 time in degrees: "
            temp=int(input(prompt))
        #Append into list
        HourlyTemperatures.append(temp)
#function to compute average of a day temperature
def ComputeAverageTemp(HourlyTemperatures):
    avg=0
    for i in range(0,len(HourlyTemperatures)):
        avg+=HourlyTemperatures[i]
    return avg/len(HourlyTemperatures)
#Function to display temperatire in column manner
#With high, low and average temperature
def DisplayTemperatures(HourlyTemperatures, AverageTemp):
    highTemp=HourlyTemperatures[0]
    lowTemp=HourlyTemperatures[0]
    print('Hour                Temperature')
    for i in range(0,len(HourlyTemperatures)):
        if(highTemp<HourlyTemperatures[i]):
            highTemp=HourlyTemperatures[i]
        if lowTemp>HourlyTemperatures[i]:
            lowTemp=HourlyTemperatures[i]
        if(i<=9):
            print('0'+str(i)+':00%16s%s'%(' ',HourlyTemperatures[i]))
        else:
             print(str(i)+':00%16s%s'%(' ',HourlyTemperatures[i]))
    print('High Temperature:    %d'%highTemp)
    print('Low Temperature:     %d'%lowTemp)
    print('Average Temperature: %d'%AverageTemp)
#Create a main function
def main():
    while(True):
        HourlyTemperatures=[]
        GetTemperatures(HourlyTemperatures)
        avg=ComputeAverageTemp(HourlyTemperatures)
        DisplayTemperatures(HourlyTemperatures,avg)
        ch=input("Do you want to test another set of data(y/n)? ")
        if(ch=="n"):
            break;
main()

------------------------------------------------------------------

Output

Enter temperature in 00:00 time in degrees: 35
Enter temperature in 01:00 time in degrees: 36
Enter temperature in 02:00 time in degrees: 36
Enter temperature in 03:00 time in degrees: 37
Enter temperature in 04:00 time in degrees: 37
Enter temperature in 05:00 time in degrees: 37
Enter temperature in 06:00 time in degrees: 38
Enter temperature in 07:00 time in degrees: 38
Enter temperature in 08:00 time in degrees: 39
Enter temperature in 09:00 time in degrees: 39
Enter temperature in 10:00 time in degrees: 40
Enter temperature in 11:00 time in degrees: 40
Enter temperature in 12:00 time in degrees: 41
Enter temperature in 13:00 time in degrees: 42
Enter temperature in 14:00 time in degrees: 43
Enter temperature in 15:00 time in degrees: 42
Enter temperature in 16:00 time in degrees: 41
Enter temperature in 17:00 time in degrees: 41
Enter temperature in 18:00 time in degrees: 40
Enter temperature in 19:00 time in degrees: 40
Enter temperature in 20:00 time in degrees: 39
Enter temperature in 21:00 time in degrees: 38
Enter temperature in 22:00 time in degrees: 35
Enter temperature in 23:00 time in degrees: 35
Hour                Temperature
00:00                35
01:00                36
02:00                36
03:00                37
04:00                37
05:00                37
06:00                38
07:00                38
08:00                39
09:00                39
10:00                40
11:00                40
12:00                41
13:00                42
14:00                43
15:00                42
16:00                41
17:00                41
18:00                40
19:00                40
20:00                39
21:00                38
22:00                35
23:00                35
High Temperature:    43
Low Temperature:     35
Average Temperature: 38
Do you want to test another set of data(y/n)? n


Related Solutions

Objective The objective of this lab exercise will be for the student to gain experience conducting...
Objective The objective of this lab exercise will be for the student to gain experience conducting a wireless networking site survey. The objective is for each group to propose a wireless networking solution for a given space (such as a classroom, office rooms, home, etc.). Verify that you can establish link (by pinging) with another laptop in your network. (Also, you may demonstrate this by downloading the shared file on another computer in the same network). Required Each individual/group will...
The objective of this lab is to practice your Verilog coding and the design of a...
The objective of this lab is to practice your Verilog coding and the design of a Finite State Machine. Lab Goal: For this lab, you will code a Verilog module to implement the FSM described in this document. This lab will also require that you use the Seven - Segment Display on the DE0 - CV FPGA board. Design Specifications for the FSM Implem ent the following simple state machine on the DE0 - CV FPGA board. This FSM will...
This problem will give you hands-on practice with the following programming concepts: • All programming structures...
This problem will give you hands-on practice with the following programming concepts: • All programming structures (Sequential, Decision, and Repetition) • Methods • Random Number Generation (RNG) Create a Java program that teaches people how to multiply single-digit numbers. Your program will generate two random single-digit numbers and wrap them into a multiplication question. The program will provide random feedback messages. The random questions keep generated until the user exits the program by typing (-1). For this problem, multiple methods...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value parameters, using Java objects, and graphics. This assignment has 2 parts; therefore you should turn in two Java files.You will be using a special class called DrawingPanel written by the instructor, and classes called Graphics and Color that are part of the Java class libraries.Part 1 of 2 (4 points)Simple Figure, or your own drawingFor the first part of this assignment, turn in a file named...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods to advance a ship in two dimensional space. Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class that models a ship flying around in a 2-D plain (containing an x and y coordinate). A game class is provided to you that will help you call methods to advance and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT