Adder

Adder

A simple adding machine.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

A “sentinel” is a dummy value that is used to terminate a `while` iteration. Most commonly used with the `input` command to mark the end of an input stream.


You have to be careful that the sentinel value is not valid data.


You can use True as a value to create an infinite loop. E.g. `while True:` will never end because True is always True. This can be useful if you don’t want your program to end unless the user closes the program.

Investigate


Questions to think about with this program to check your understanding:

Relation question

Explain why line 13:

number = float(input(“Enter number: “))

must be after line 12:

while not complete:

and not before.

REVEAL ANSWER

You want the user to be able to keep entering numbers until they enter zero, so the input must be inside the loop.

Approach question

What is the problem with using zero as the sentinel value in line 14?

REVEAL ANSWER

It means that zero can never be input as a valid number. This is a problem if you want the data set to include zeros for the purpose of calculating the average. A better approach would be to input a number as a string and cast it to an integer after checking if the value is an empty string first. The empty string then becomes the sentinel value and not the number zero.

Make


Success Criteria

Change the program so that it:

  1. Uses the letter “e” as the sentinel value instead of zero in line 14. Note you will need to change other lines to make this work too because a letter cannot be cast to a float in line 13.
  2. Outputs the lowest (bottom) number too.

Hint: the lowest number will need to be initialised to be the first number that is input.

Typical inputs and outputs from the program would be:

Enter number: 4

Enter number: 2

Enter number: 8

Enter number: 6

Enter number: e

Total: 20.0 Top: 8.0 Bottom: 2.0 Ave: 5.0

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Enter number: 50

Enter number: 40

Enter number: 30

Enter number: 60

Enter number: 70

Enter number: 80

Enter number: e

Total: 330.0 Top: 80.0 Bottom: 30.0 Ave: 55.0

Enter number: 10

Enter number: 20

Enter number: 30

Enter number: 40

Enter number: 50

Enter number: e

Total: 150.0 Top: 50.0 Bottom: 10.0 Ave: 30.0

Enter number: e

Total: 0 Top: 0 Bottom: 0 Ave: 0

Enter number: -76.3

Enter number: 234

Enter number: -9.7

Enter number: 45

Enter number: 16

Enter number: 0

Enter number: 4

Enter number: 0

Enter number: e

Total: 213.0 Top: 234.0 Bottom: -76.3 Ave: 26.625

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist