really struggling with this simple problem below. struggling to code a line of code that include all the possible cases.
any suggestion would be much appreciated
the code has to print True if
feel_prepared = True
and
review_time > = 2
But if test_percent >=20
than we need a review_time
of at least 4 hours to get a TRUE
test_percent = 21
review_time = 2
feel_prepared = True
print(prepared and (test_percent >= 20 or test_percent <20) and review_time >=4))
but doesn't work if feel_prepared = True
, review_time = 2
and test_percentage \<20
CodePudding user response:
One approach would be to break this problem into smaller ones, and solve it iteratively.
Let's review the first part of the exercise:
the code has to print True if feel_prepared = True and review_time > = 2
So you pass the test when feel_prepared == True and review_time >= 2
.
Now for the second part:
But if test_percent >=20 than we need a review_time of at least 4 hours to get a TRUE
What it says is that in some cases you pass the test with feel_prepared == True and review_time >= 4
You can pass with either feel_prepared == True and review_time >= 2
or feel_prepared == True and review_time >= 4
. The only difference is test_percent
.
What the exercise text says is when test_percent
is lower than 20
, you pass with feel_prepared == True and review_time >= 2
, and when it's equal or higher than 20
, you pass with feel_prepared == True and review_time >= 4
.
So if it's test_percent < 20 and feel_prepared == True and review_time >= 2
, you pass. Or you pass when it's test_percent >= 20 and feel_prepared == True and review_time >= 4
.
Thus,
(test_percent < 20 and feel_prepared == True and review_time >= 2) or (test_percent >= 20 and feel_prepared == True and review_time >= 4)
You can then apply distributive law and write
feel_prepared == True and ((test_percent < 20 and review_time >= 2) or (test_percent >= 20 and review_time >= 4))
You can also notice that when you feel_prepared
and review_time >= 4
, you always pass. But when 2 <= review_time < 4
, you pass only if test_score < 20
:
feel_prepared == True and (review_time >= 4 or (review_time >= 2 and test_percent < 20))
CodePudding user response:
I think this solves the problem:
test_percent = 19
review_time = 2
feel_prepared = True
statement = feel_prepared and ((test_percent >= 20 and review_time >=4) or (test_percent < 20 and review_time >=2))
print(statement) #True because test_percent < 20 and review_time >= 2
test_percent = 21
review_time = 2
feel_prepared = True
statement = feel_prepared and ((test_percent >= 20 and review_time >=4) or (test_percent < 20 and review_time >=2))
print(statement) #False because test_percent >= 20 and review_time < 4
CodePudding user response:
I believe your problem is with the parenthesis, and fact that (test_percent < 20 or test_percent >= 20) always return True.
I don't know if I understood correctly all the cases, but I think this is the correct :)
print((prepared and review_time >=2 and test_percent < 20) or (prepared and review_time >=4 and test_percent >= 20)