Decision Making
If..
The If.. control structure allows computer programs to make decisions based on the data that is stored in the computer. A good example of getting the computer to make decisions is to find the maximum or minimum values of data that is entered.
The general form is:
If condition:
… statements
Condition controls the outcome and must be a Boolean expression. An example would be Age < 6.
Consider the following problem
A student has a Social Science project for which the maximum temperature for an 8 hour period of time must be found. The student has a thermometer and notes the temperature every hour. You are asked to write a program that will accept the temperatures noted over the day and then determine the maximum temperature.
#Determine maximum temperature written in Python
print 'Maximum Temperature' #program heading
#initialise maximum temperature
maximum = 0
#use the range function to set up team list of 8 hours players
time = range(1,9)
for hour in time:
temperature = int(raw_input('Enter the temperature for hour ' + repr(hour) + ' -> '))
if temperature > maximum:
maximum = temperature
print 'The maximum temperature so far is %d' %(maximum)
print 'You have finished.'
If .. Else
In the above example, the result of the decision made some action, or nothing to happen. The command can be used to make an alternate action happen as well, which is the norm.
The general form is:
If condition:
… statements
Else:
... statements
Condition controls the outcome and must be a Boolean expression. An example would be Age < 6.
The following example will demonstrate how this is done.
The Australian Taxation office allows some people to claim use of their private car. Cars which have engine capacities 1500 cc and over are allowed 65c per kilometer travelled, while those with engine capacities up to 1500cc are allowed 45c per kilometer because they are smaller. A program is needed to calculate the allowances.
#Calculating travel cost written in Python
print 'Tavel cost calculator' #program heading
#get distance and capacity
distance = int(raw_input('Enter the distance travelled -> '))
capacity = int(raw_input('Enter the engine capacity of your car -> '))
if capacity > 1500:
allowance = distance * 0.65
else:
allowance = distance * 0.45
#show allowance
print 'The allowance is $%5.2f' %(allowance)
print 'You have finished.'
Note:
The print formatting %5.2f forces a floating point number with 2 decimal places
Nested If..Then..Else
In a lot of situations, a number of comparisons based on the input must be made. The solution requires the if....else structure within other elif statements in between. This is called nesting, and can become very complex very quickly.
The following example will demonstrate how this is done.
Parcel-Post Airfreight charges for parcel delivery based on weight. The following is a table of their charges.
| Weight |
Price |
| < 2 kg |
$3.50 |
| 2Â-5kg |
$5.50 |
| >5kg |
$6.50 |
A program is required that will calculate the cost due when the weight is entered. It is necessary to check to see which category the weight falls in. If Weight is not less than 2, it must be checked again to see whether it is less than 5 or greater then 5. The charge, Charge, is calculated based on final rate.
#Calculating Air freight cost written in Python
print 'Air Freight cost calculator' #program heading
weight = int(raw_input('Enter the weight of the parcel -> '))
if weight < 2:
cost = 3.5
elif weight > 5:
cost = 6.5
else:
cost = 5.5
print 'The cost to send the parcel is $%5.2f' % cost
print 'You have finished.'
Exercises
-
In the diving competition of World Diving Championship, 7 judges award a score to each competitor. So as the judging will be fair, a diver's final score is calculated by deducting the highest and lowest score after adding all of the scores together. You have been commissioned to create a program that could be used to calculate the score of a diver.
-
You have been contracted to write a Wages Calculator program by Freddies Fast Food. Juniors under 18 get $15.60 an hour and the rest get $17.80 an hour. Write the program to calcule the wage of an employee based on the age and number of hours worked in a week.
-
A CD manufacturing company charges Multimedia companies $6.50 per CD if they create less than 1000 copies. If the company requires more than 10000, they only pay $5.00 a copy. Otherwise, they pay $5.70 a copy. Write a program that will allow the company to calculate how much to charge for different jobs they do.
-
Modify the simple guessing game example to provide advice to the user as to whether their guess was too high or too low.
-
Modify Q4 above to provide information to the user that will tell him/her how close they are to the secret number - use hot, warm and cold as a clue
[Python_Tutorial] [Sequence_Python] [Interation_Python] [Decision_Making_Python] [Arrays_Python] [Files_Python]
Comments (0)
You don't have permission to comment on this page.