While loop activities

posted by: Ms. Martin 6 April 2010 2 Comments

While loops or indefinite loops are a very important programming construct — they allow us to continue executing code until a particular condition stops being true. (See the Text book section: While Loops).

You will complete activities 1 and 2 and your choice of 3 or 4.  Write all of these in a single file with each activity in its own function.

Activity 1: Countdown

Write a program that asks the user for a number, and prints a countdown from that number to zero. What should your program do if the user inputs a negative number? As a programmer, you should always consider “edge conditions” like these when you program! (Another way to put it- always assume the users of your program will be trying to find a way to break it! If you don’t include a condition that catches negative numbers, what will your program do?)

Activity 2:

Write a function named dice_sum.  This function should prompt the user for a desired sum then repeatedly roll two six-sided dice until their sum is the desired sum.  Here is what a run of your program should look like (user input underlined):

What sum are you looking for? 6
Rolled a 4 and a 4
Rolled a 6 and a 5
Rolled a 2 and a 6
Rolled a 5 and a 6
Rolled a 4 and a 2
Got a 6 in 5 rolls!

Activity 3: Multiplication tutor

Feel free to take some freedom with the messages displayed to users!

You are trying to help your younger sibling/cousin/neighbor learn to multiply. Your task is to write a multiplication tutor. Your game should display a welcome message including the player’s name. It should then randomly generate two numbers and ask the player to type out their product. The player should get one point for every correct answer. The game should keep going until the user types in -1. After the game is over, the player’s score should be displayed.

Here is what your game should look like — user input is underlined:

Welcome to the multiplication tutor!
What is your name? Ms. Martin
Let’s see how good you are at multiplication, Ms. Martin.
Type -1 at any time to exit and see your score
What is 5 * 8? 40
Correct!
What is 8 * 8? 64
Correct!
What is 7 * 10? 77
NO! The answer was 70
What is 7 * 9? 9
NO! The answer was 63
What is 5 * 5? 25
Correct!
What is 9 * 9? -1

Thanks for playing, Ms. Martin! You scored 3 points.

Activity 4: Rock, paper, scissors

You will write a program to simulate play rock, paper, scissors against the computer.  The rules of rock paper scissors are represented in the following diagram:
Your program will use numbers between one and three to represent the three possible plays.  You will need two variables: one to keep track of the computer’s play and one to keep track of the user’s play.  Use conditionals to figure out who wins.  See a sample run of the program below:
Welcome to rock paper scissors!
What is your name? Ms. Martin
Type -1 at any time to exit and see your score
What do you want to play? 1) rock 2) paper 3) scissors 3
Draw
What do you want to play? 1) rock 2) paper 3) scissors 2
Sorry, computer wins!
What do you want to play? 1) rock 2) paper 3) scissors 5
Invalid input!!
What do you want to play? 1) rock 2) paper 3) scissors 1
You win!
What do you want to play? 1) rock 2) paper 3) scissors 2
Draw
What do you want to play? 1) rock 2) paper 3) scissors -1
Thanks for playing, Ms. Martin!  You scored 1 points.
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>