190k views
5 votes
Define a function calculate_range that consumes a temperature and returns a tuple of two numbers, the high and the low. These numbers are calculated as follows:

high = temperature + 20
low = temperature - 30
Then, you will need to call your function once to create two variables today_low and today_high for today's temperature of 70.
Note: Your function will be unit tested against multiple arguments. It is not enough to get the right output for your own test cases, you will need to be able to handle any kind of number.
Note: You are not allowed to use list or tuple indexing.
Note: You may only call your function once.

1 Answer

1 vote

Answer:

def calculate_range(temperature):

high = temperature + 20

low = temperature - 30

return low, high

today_temp = 70

today_low , today_high = calculate_range(today_temp)

print('Today\'s low = {}\\Today\'s high = {}'.format(today_low, today_high))

Step-by-step explanation:

The programming language used is python.

The code define the calculate_range function that takes only one parameter temperature.

The function evaluates both high and low temperature and returns a tuple containing both values.

we test the function by creating a temperature variable for today and passing it to the function and this in turn returns two values today_low and today_high.

Your question specifies no need for indexing.

There is a really simple way of unpacking tuple objects, which is used to assign the values returned by the function to separate variables.

today_low , today_high = calculate_range(today_temp)

Finally, today_low and today_high is printed to the screen.

check the picture to see results of running the code.

Define a function calculate_range that consumes a temperature and returns a tuple-example-1
User Xmike
by
4.9k points