Moh'd H
Moh'd H

Reputation: 247

Boolean expressions: function not returning expected output

Question:

Write a function should_shutdown(battery_level, time_on) which returns True if the battery level is less than 4.8 except when the time_on is less than 60, in which case the function returns True only if the battery level is less than 4.7. In all other situations the function returns False.

def should_shutdown(battery_level, time_on):
    if battery_level < 4.8:
        if time_on < 60:
            return False
        else:
            return True
    else:
        if battery_level < 4.7:
            return True
        else:
            return False

Testings:

should_shutdown(4.69, 50) Evaluates to: 'False'. Should return "True"

should_shutdown(5, 10) Evaluates to: False

should_shutdown(4.74, 90) Evaluates to: True

should_shutdown(4.74, 50) Evaluates to: False

should_shutdown(4.7, 50) Evaluates to: False

should_shutdown(4.75, 60) Evaluates to: True

should_shutdown(4.75, 59) Evaluates to: False

The first test is not returning the expected output by the quiz server. i don't understand the question very much so that's why I've copy pasted it.

Upvotes: 0

Views: 256

Answers (2)

John1024
John1024

Reputation: 113814

Note that:

  1. When battery_level < 4.7, the function should always return True.

  2. When battery_level < 4.8 and time_on is not less than 60, the function should also return True.

Putting these two together yields:

def should_shutdown(battery_level, time_on):
    return (battery_level < 4.7) or (battery_level < 4.8 and time_on >= 60)

Upvotes: 2

gwhiz
gwhiz

Reputation: 623

def should_shutdown(battery_level, time_on):
    if battery_level < 4.8:
        return (time_on >= 60) or (time_on < 60 and battery_level < 4.7)
    return False

This should be the function for the above question

Upvotes: 1

Related Questions