Skip to content

What do you do when you have a problem using 'if' option after generating a random number? #21423

Discussion options

You must be logged in to vote
asdklfjsad:

This only prints ‘good’ when ‘a’ isnt in range.

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:

asdklfjsad:

if (0<=a<=17)

I’ll add parentheses to show what actually happens here:

if ((0 <= a) <= 17)

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.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants