Exercise 2.5. (Python Nested Selection Constructs)
1. Ensure you have viewed the video (associated with this exercise) at this link >>
2. Consider the following specification and the program that implements this specification.
Specification
Write a program that asks users to enter their age. If they are old enough to vote the program asks them to collect their polling card. However, if they are not old enough to vote the program tells them that they are too young to vote. You must be 18 years of age or older to vote.
Specification
Write a program that asks users to enter their age. If they are old enough to vote the program asks them to collect their polling card. However, if they are not old enough to vote the program tells them that they are too young to vote. You must be 18 years of age or older to vote.
The program that implements the above specification
Produce a simple test plan for the computer program above. Ensure that the test plan is based upon boundary testing. Enter the above program into your editor (copy and paste) and test the runtime against the test plan. Try and avoid referring to the video while completing this exercise.
3. The specification above is amended with the following additional requirement. If the user enters their age as 17, they are still told that they are too young to vote but in addition they are told they can vote next year.
Amend the program shown in the last exercise so that it accommodates this additional requirement. You are asked to ensure that this additional requirement is implemented in the code by another if … Selection construct that is suitably nested. Again try and avoid referring to the video while completing this exercise.
Amend the program shown in the last exercise so that it accommodates this additional requirement. You are asked to ensure that this additional requirement is implemented in the code by another if … Selection construct that is suitably nested. Again try and avoid referring to the video while completing this exercise.
4. Implement the following specification using Python.
Specification
Individuals who are 65 or over can apply for a concessionary travel card. Write a computer program that asks the user for their age and if they are not old enough then the following string is displayed:
Sorry you are not old enough to apply for a concessionary travel card.
However, if they are old enough the following string is displayed:
You may apply for the concessionary travel card.
It would be sensible to proceed as follows: Produce a Design (Structured English or Nassi–Shneiderman Diagram), a Data Table, Test plan with full boundary testing, and a Python program. Test your Python program runtime against the Test Plan. However, you may feel confident enough to go straight to the code for this specification. I will leave the decision to you, however, make sure you fully test the program using boundary testing.
Specification
Individuals who are 65 or over can apply for a concessionary travel card. Write a computer program that asks the user for their age and if they are not old enough then the following string is displayed:
Sorry you are not old enough to apply for a concessionary travel card.
However, if they are old enough the following string is displayed:
You may apply for the concessionary travel card.
It would be sensible to proceed as follows: Produce a Design (Structured English or Nassi–Shneiderman Diagram), a Data Table, Test plan with full boundary testing, and a Python program. Test your Python program runtime against the Test Plan. However, you may feel confident enough to go straight to the code for this specification. I will leave the decision to you, however, make sure you fully test the program using boundary testing.
5. For the specification of 4. (above) add the following requirement. If the user enters the age 64 the following is to be displayed:
Sorry you are not old enough to apply for a concessionary travel card.
You can apply on or after your next birthday.
Implement the program by inserting another if … Selection construct suitably nested within the code you produced for 4. (above).
Sorry you are not old enough to apply for a concessionary travel card.
You can apply on or after your next birthday.
Implement the program by inserting another if … Selection construct suitably nested within the code you produced for 4. (above).
6. For each of the following nonsense programs workout through reading the code the output of each program i.e. do not just execute the program and observe the output.
Exercise 2.5 Example 2
Exercise 2.5 Example 3
Exercise 2.5 Example 4
Exercise 2.5 Example 5
You can check your own answers to the question by copying the code from this webpage, pasting it into your Python editor and executing the program.
Note: To copy the code from webpages on this website place your cursor in the code area, right click the mouse to bring the context menu into view. Select Select all from the context menu this should highlight the code. Ensure the code remains highlighted and click on the right mouse button again and this time select Copy from the context menu that appears. Now paste the copied code into your Python editor and execute the program.
END Next Exercise >>