CSE 110 | Programming Building Blocks

07 Prepare: Checkpoint

Overview

Demonstrate your understanding of loops by completing the following individual checkpoint assignment.

Assignment

Write a Python Program that does each of the following:

  1. Use a while loop to ask the user for a positive number (>= 0). Continue asking as long as the number is negative, then display the number. For example:

  2. 
    Please type a positive number: -3
    Sorry, that is a negative number. Please try again.
    Please type a positive number: -8
    Sorry, that is a negative number. Please try again.
    Please type a positive number: -1
    Sorry, that is a negative number. Please try again.
    Please type a positive number: 12
    The number is: 12
    
  3. Use a while loop, to simulate a child asking their parent for a piece of candy. Have the program keep looping until the user answers "yes", then have the program output "Thank you." For example:

  4. 
    May I have a piece of candy? no
    May I have a piece of candy? no
    May I have a piece of candy? no
    May I have a piece of candy? no
    May I have a piece of candy? yes
    Thank you.
    

Sample Solution

When your program is finished, please view a sample solution of this program to compare your approach to that one.

You should work to complete this checkpoint program first, without looking at the sample solution. However, if you have worked on it for at least an hour and are still having problems, you may feel free to use the sample solution to help you finish your program.

Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. For the positive number program, enter several negative numbers, then a positive one.

  2. Try entering a positive number first, and ensure that it doesn't ask again.

  3. Try entering 0 and ensure that it correctly treats it as a positive number.

  4. For the piece of candy program, test your last loop by entering different amounts of no's before finally saying yes.

  5. Try saying "yes" the very first time, and ensure that it works as expected.

Submission

When complete, please report your progress in the associated I-Learn quiz.