On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

## Condition Exercises

1. Develop an “or-guard”. In the example above we showed the “and-guard” pattern:

```average = count != 0 and float(sum)/count
```

Develop a similar technique using or .

Compare this with the if-else operator.

2. Come Out Win. Assume `d1` and `d2` have the numbers on two dice. Assume this is the come out roll in Craps. Write the expression for winning (7 or 11). Write the expression for losing (2, 3 or 12). Write the expression for a point (4, 5, 6, 8, 9 or 10).

3. Field Win. Assume `d1` and `d2` have the numbers on 2 dice. The field pays on 2, 3, 4, 9, 10, 11 or 12. Actually there are two conditions: 2 and 12 pay at one set of odds (2:1) and the other 5 numbers pay at even money. Write two two conditions under which the field pays.

4. Hardways. Assume `d1` and `d2` have the numbers on 2 dice. A hardways proposition is 4, 6, 8, or 10 with both dice having the same value. It's the hard way to get the number. A hard 4, for instance is ```d1+d2 == 4 and d1 == d2```. An easy 4 is `d1+d2 == 4 and d1 != d2`.

You win a hardways bet if you get the number the hard way. You lose if you get the number the easy way or you get a seven. Write the winning and losing condition for one of the four hard ways bets.

5. Sort Three Numbers. This is an exercise in constructing if-statements. Using only simple variables and if statements, you should be able to get this to work; a loop is not needed.

Given 3 numbers ( `X` , `Y` , `Z` ), assign variables `x`, `y`, `z` so that `x``y``z` and `x`, `y`, and `z` are from `X` , `Y` , and `Z` . Use only a series of if-statements and assignment statements.

Hint. You must define the conditions under which you choose between `x` `X` , `x` `Y` or `x` `Z` . You will do a similar analysis for assigning values to `y` and `z`. Note that your analysis for setting `y` will depend on the value set for `x`; similarly, your analysis for setting `z` will depend on values set for `x` and `y`.

6. Come Out Roll. Accept `d1` and `d2` as input. First, check to see that they are in the proper range for dice. If not, print a message.

Otherwise, determine the outcome if this is the come out roll. If the sum is 7 or 11, print winner. If the sum is 2, 3 or 12, print loser. Otherwise print the point.

7. Field Roll. Accept `d1` and `d2` as input. First, check to see that they are in the proper range for dice. If not, print a message.

Otherwise, check for any field bet pay out. A roll of 2 or 12 pays 2:1, print "pays 2"; 3, 4, 9, 10 and 11 pays 1:1, print "pays even"; everything else loses, print "loses"

8. Harways Roll. Accept `d1` and `d2` as input. First, check to see that they are in the proper range for dice. If not, print a message.

Otherwise, check for a hard ways bet pay out. Hard 4 and 10 pays 7:1; Hard 6 and 8 pay 9:1, easy 4, 6, 8 or 10, or any 7 loses. Everything else, the bet still stands.

9. Partial Evaluation. This partial evaluation of the and and or operators appears to violate the evaluate-apply principle espoused in The Evaluate-Apply Cycle. Instead of evaluating all parameters, these operators seem to evaluate only the left-hand parameter before they are applied. Is this special case a problem? Can these operators be removed from the language, and replaced with the simple if -statement? What are the consequences of removing the short-circuit logic operators?

 Published under the terms of the Open Publication License Design by Interspire