Question

In: Computer Science

A few weeks ago you crated a program which randomly generated a wind-speed, and the output...

A few weeks ago you crated a program which randomly generated a wind-speed, and the output told us what category of cyclone it was. Your solution may have looked like this:
import random

def main():
    wind_speed = random.randint(0, 255)

    if wind_speed in range(0, 118):
        message = str(wind_speed) + "\n It is not yet a cyclone"
    elif wind_speed in range(119, 153):
        message = str(wind_speed) + "\n It is a Category 1 cyclone"
    elif wind_speed in range(154, 177):
        message = str(wind_speed) + "\n It is a Category 2 cyclone"
    elif wind_speed in range(178, 208):
        message = str(wind_speed) + "\n It is a Category 3 cyclone"
    elif wind_speed in range(209, 251):
        message = str(wind_speed) + "\n It is a Category 4 cyclone"
    else:
        message = str(wind_speed) + "\n It is a Category 5 cyclone"

    print(message)

main()

Now we need to refactor this code. Extract the decision into a function called det_category, which takes a number (the wind speed) as an input. This function needs to return the message.
main() should still generate the number, and display the message. It will need to call the det_category function in between those two things.

Solutions

Expert Solution

# Python code

import random
"""
det_category, which takes a number (the wind speed) as an input. 
This function needs to return the message.
"""
def det_category(wind_speed):
    if wind_speed in range(0, 118):
        message = str(wind_speed) + "\n It is not yet a cyclone"
    elif wind_speed in range(119, 153):
        message = str(wind_speed) + "\n It is a Category 1 cyclone"
    elif wind_speed in range(154, 177):
        message = str(wind_speed) + "\n It is a Category 2 cyclone"
    elif wind_speed in range(178, 208):
        message = str(wind_speed) + "\n It is a Category 3 cyclone"
    elif wind_speed in range(209, 251):
        message = str(wind_speed) + "\n It is a Category 4 cyclone"
    else:
        message = str(wind_speed) + "\n It is a Category 5 cyclone"
    return message

# main() function


def main():
    # generate the number, and display the message.
    wind_speed = random.randint(0, 255)
    # call the function
    message = det_category(wind_speed)

    print(message)
# call function
main()

# screenshot for indentation help

#output

// if you need any help regarding this solution ................ please leave a comment .......... thanks


Related Solutions

8. You started a new full-time job a few weeks ago and you just learned that...
8. You started a new full-time job a few weeks ago and you just learned that your family owns part of a business that provides services to your employer. What should you do? Quit your job immediately. Disclose the conflict of interest to your manager. Report the situation to a third-party committee. 9. Which of the following signs indicate weak ethical culture in an organization? Select ALL relevant signs. The company has a channel for employees to report misconduct anonymously....
A few weeks ago, you shorted sell 100 shares of Gemini Inc. at $60 per share....
A few weeks ago, you shorted sell 100 shares of Gemini Inc. at $60 per share. Your dealer requires 50% initial margin with 25% maintenance margin. Today, the price of the stock is $71 per share. Given this scenario: (1) What is your Total Account Value today (i.e how much total cash and value of the shares are in your account) and (2) What is your Total dollar gain/loss on the shorted shares (ignoring fees, charges and margin) A. (1)...
You want to conduct an experiment to understand how the power generated by a wind turbine...
You want to conduct an experiment to understand how the power generated by a wind turbine is affected by the speed of the wind. Write your hypothesis (1point) and explain how you can conduct the experiment (assume that you’re in laboratory conditions like we preformed the Wind Turbine experiment) eg the experiment needed and the procedure of the experiment (1 point). Which parameters/conditions in the experiment should be kept under control and which parameters should be observed to test the...
You want to conduct an experiment to understand how the power generated by a wind turbine...
You want to conduct an experiment to understand how the power generated by a wind turbine is affected by the speed of the wind. Write your hypothesis (1point) and explain how you can conduct the experiment (assume that you’re in laboratory conditions like we preformed the Wind Turbine experiment) eg the experiment needed and the procedure of the experiment (1 point). Which parameters/conditions in the experiment should be kept under control and which parameters should be observed to test the...
11. In Donald Trump’s victory speech a few weeks ago, he claimed to pursue massive fiscal-stimulus...
11. In Donald Trump’s victory speech a few weeks ago, he claimed to pursue massive fiscal-stimulus policies centered on infrastructure spending and tax cuts for both corporations and the workers. Given a current unemployment rate of 4.9% and a projected annual economic growth rate of 2.4% for 2016, do you think the proposed fiscal policy by Trump will benefit the economy? Use an AS-AD diagram to illustrate both the short run and long run effects. (Hint: the estimated natural rate...
In a few more weeks, you will be adding a new member to your family –...
In a few more weeks, you will be adding a new member to your family – a 10-week old golden retriever puppy! In previous litters, the average weight of 42 puppies at 10 weeks was 14.8 lbs with a standard deviation of 1.1 lbs. Find the 90% confidence interval for the average 10-week weight for golden retriever puppies. Show/explain your work, and identify the following: a. The estimated population mean and degrees of freedom b. The assumptions you are making...
You have been working with the arrays for the past few weeks and you have learned...
You have been working with the arrays for the past few weeks and you have learned their limitations. In this assignment, you will be using a new data structure, HashMaps, to repeat Assignment 8.3. Main Method (5 points) Declare and initialize an HashMap with values of type of type Employee, and keys of type integer. In a while loop, keep initializing objects of type Employee, storing them in the HashMap, and allow the user to enter ID, salary, and leaveDays...
Two weeks ago, you were playing with a guitar at home – an instrument that you...
Two weeks ago, you were playing with a guitar at home – an instrument that you have been playing for ten years. Your mother came back from work and began talking to you nonstop. In this case, you were able to hear everything she said. Last night, you were studying for a difficult final exam in philosophy. Your mother gave you the same speech again when she came home, but this time you were not able to hear anything she...
You are a middle level manager and during the last few weeks you have experienced several...
You are a middle level manager and during the last few weeks you have experienced several communication problems which have made your job more difficult. Reflecting on the information in chapter 12, you realize there could and should be changes in your company to improve communication. You will write a memo to your boss, the V.P. In this (minimum of 400 word) memo you will: o   describe the importance of effective communication in the organization o   identify three specific and...
You are the controller for Bizbee Corporation, and a few days ago, you provided a draft...
You are the controller for Bizbee Corporation, and a few days ago, you provided a draft of this year's financial statements to the chief executive officer (CEO) of the company, Mr. Bizbee. You rode up in the elevator with him today, and he began to quiz you about how you reported the company's investments in debt and equity securities. He said to you, "When I took accounting in college, investment securities were reported at historical cost. I remember what we...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT