Exercise 2.2 (The Python if … Selection Construct)
1. Ensure you have viewed the video (associated with this exercise) at this link >>
2. People who are asked if today is Monday would usually reply with either yes or no depending on whether it was or was not a Monday. Programmers frequently arrange for their programs to ask similar questions, but they do not think in terms of yes or no. What words do programmers usually use when talking about questions that have one of two possible responses?
3. This symbol < is an example of what type of operator?
4. Which of the following appropriately placed in a Python program gives True and which gives False?
- 7 < 7
- 6 < 7
- 8 < 7
- 998 < 234
5. How many spaces are recommended for applying indentation in a Python program?
6. For the flowchart below is the bank charge applied?
7. Follow the logic implied by the flowchart below and indicate what current account balance would be displayed.
8. What is displayed when the programs below are executed? Once you have answered this check your answers by copying the programs in turn, pasting them into your Python editor and executing them.
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.
Exercise 2.2. Example 1
Exercise 2.2. Example 2
Exercise 2.2. Example 3
Exercise 2.2. Example 4
9. For the following specification produce a Structured English Design, a Data Table, and a Simple Test Plan. Convert the design to a Python program, enter it into your editor and test its runtime against the Test Plan.
Specification
Read two numbers from the keyboard and multiply them together (i.e. find the product). If the product is less than 100 then the program reads another number from the keyboard and adds this to the product. However, if the product is greater than or equal to 100 then another number is not read from the keyboard.
Arrange for the last line of the program to display the answer.
The answer may be the product of the first two numbers entered or the product with the addition of a third number.
Specification
Read two numbers from the keyboard and multiply them together (i.e. find the product). If the product is less than 100 then the program reads another number from the keyboard and adds this to the product. However, if the product is greater than or equal to 100 then another number is not read from the keyboard.
Arrange for the last line of the program to display the answer.
The answer may be the product of the first two numbers entered or the product with the addition of a third number.
END Next Exercise >>