python 3.x - Boolean operators and Conditions -
python 3.x - Boolean operators and Conditions -
i'm struggling code running perfectly.
question:
write function should_shutdown(battery_level, time_on) returns true if battery level less 4.8 except when time_on less 60. if not case, function returns true if battery level less 4.7. in other situations function returns false.
my code:
def should_shutdown(battery_level,time_on): if battery_level , time_on < 60: homecoming false elif battery_level < 4.8: homecoming true else: homecoming false
it works fine, except when test following:
ans = should_shutdown(4.69, 50) print(ans)
it returns false
, right reply should true
.
other tests:
should_shutdown(5, 10) expected: false got: false
should_shutdown(4.74, 90) expected: true got: true
should_shutdown(4.74, 50) expected: false got: false
should_shutdown(4.7, 50) expected: false got: false
- should_shutdown(4.69, 50) expected: true got: false
should_shutdown(4.75, 60) expected: true got: true
should_shutdown(4.75, 59) expected: false got: false
def should_shutdown(battery_level, time_on): if battery_level < 4.8: if time_on < 60: homecoming false else: homecoming true else: if battery_level < 4.7: homecoming true else: homecoming false
your problem lies in first if-statement:
if battery_level , time_on < 60:
this same as
if battery_level!=0 , time_on<60:
this because in python, number not 0
evaluates true
. perhaps meant:
if battery_level<4.8 , time_on < 60:
python-3.x condition boolean-operations
Comments
Post a Comment