What do you do when you have a problem using 'if' option after generating a random number? #21423
-
This only prints ‘good’ when ‘a’ isnt in range. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
asdklfjsad:
This actually always prints “good”, and it has nothing to do with the random number. The reason is that you combine expressions in the
asdklfjsad:
I’ll add parentheses to show what actually happens here:
The result of If you want to make sure that a is in the range from 0 to 17 (inclusive) you need to check both conditions separately and combine them with a |
Beta Was this translation helpful? Give feedback.
-
I understand! thanks |
Beta Was this translation helpful? Give feedback.
This actually always prints “good”, and it has nothing to do with the random number. The reason is that you combine expressions in the
if
condition:I’ll add parentheses to show what actually happens here:
The result of
0 <= a
is always either 0 (false) or 1 (true). And both 0 and 1 are less than or equal to 17. 🙂If you want to make sure that a is in the range from 0 to 17 (inclusive) you need to check both conditions separately and combine them with a
&&
(logical and) operator.