In the previous chapter, there was a situation where two if statements could both be true at the same time. We can make compound conditions with and or or to prevent this.
Both parts of an and condition must be true. Either part of an or condition must be true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
How old is the human? 42 A human 42.0 years old is usually called 'adult'.
The output will depend on what you type, of course.
If they type a negative number for the age or something very old, we will complain about it.
Then if the age is at least zero but less than one, we set the variable term to the string "baby".
If the age is at least one but less than three, we set it to "toddler". And so on.
- Add another
ifstatement in there somewhere. And make sure theifstatement after the one you added gets changed, too. Test your program with ages near the range that you chose to make sure everything works correctly.
©2017 Graham Mitchell