Reputation: 35
So I'm trying to validate something to complete my coursework but it has been giving me these errors:
R:197, 4: The if statement can be replaced with 'return bool(test)' (simplifiable-if-statement)
R:197, 4: Unnecessary "else" after "return" (no-else-return)
I'm generally new to coding so I'm not entirely sure on how to implement the return bool(test) into my code. Hoping you guys could help me? I've found out that python is really sensitive with indentation, which is why I'm kind of iffy about the second error. Here is the code:
def in_range(number):
"""
True or false
"""
if number > 50 and number < 100:
return True
else:
return False
answer = in_range(90)
Upvotes: 0
Views: 596
Reputation: 8069
When python interprets if number > 50 and number < 100:
, a result of expression number > 50 and number < 100
converts to bool
and then executes first or second block (return True or False in your case). So you can simplify the statement just returning number > 50 and number < 100
.
def in_range(number):
"""
True or false
"""
return number > 50 and number < 100
answer = in_range(90)
But it seems that you don't need a function here:
answer = bool(100 > number > 50)
Upvotes: 0
Reputation: 3
They're not fatal error messages, and should be quite easily understandable. All the interpreter is saying is that the algorithm can be better written as
def in_range(number):
return 50 < number < 100
Upvotes: 0
Reputation: 1036
The phrase number > 50 and number < 100
evaluates to a boolean. If you simplify that phrase it will be True
or False
, so it's redundant to effectively say if True: return True
. You can simplify the if-statement as a result.
Upvotes: 0
Reputation: 3698
You can shorten that to:
def in_range(number):
"""
True or false
"""
return 50 < number < 100
answer = in_range(90)
print(answer)
...or, if you are required to use bool()
nonetheless, just wrap it:
return bool(50 < number < 100)
Upvotes: 1