LESSON 06 – WHILE LOOPS

 


WORK


GROUP PROGRAM – NUMBER GUESSING GAME

 

As a class, we will create the pseudocode for a number guessing game.  Then, you will have to create that game in Python.

 

PROGRAM 2 – FOR TO WHILE

 

Alter the following program to use a while loop instead of a for loop.

 

for x in range(0,5):
    print(x)

PROGRAM 3 – WHILE TO FOR

 

Alter the following program to use a for loop instead of a while loop:

 

t=5

while t<15:

    print(t)

    t=t+2

 

PROGRAM 4 – LIST OF WORDS (SCATEGORIES)

 

Write a program that randomly selects a letter from the alphabet.

 

It then continuously asks the user for a word.  If the word starts with the letter, the program gives the user a point for each letter in the word.

 

However, if the word doesn’t start with the letter, the program stops executing and outputs the total score.

 

NOTE: The game will use the honour system and trust that real words are being entered.

 

HINT #1 – GETTING A RANDOM LETTER

Create a list with all letters.  Generate a random number from 0 to 25.  Pick that letter.

 

HINT #2 – CHECKING FIRST LETTER OF A STRING

Let’s assume we have a string called name.  We can check if its first letter is "P" by using the following if statement condition:

 

     if name[0] == "P":

 

HINT #3 – LENGTH OF A STRING

We can easily check how many characters are in a string by using the len() function.  For a string called name, we use:

 

     count = len(name)