Difference between revisions of "Guessing Game Assignment"

From WLCS
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
'''Objectives:'''
 +
* The learner will be able to generate a random number
 +
* The learner will be able to use if-statements
 +
* The learner will be able to implement a while loop
 +
* The learner will be able to nest if-statements inside a while loop
 +
 +
'''Resources:'''
 +
* [[Media:PythonWhileLoops.pptx]]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration]
 +
 
'''Guessing Game Rules'''
 
'''Guessing Game Rules'''
 
# Mr. Bui will think of a number from 0 to 100
 
# Mr. Bui will think of a number from 0 to 100
Line 5: Line 15:
 
# You will guess again, and so on and so forth.  The above process will only be repeated 7 times.  If you do not guess the number within 7 times, then you lose.
 
# You will guess again, and so on and so forth.  The above process will only be repeated 7 times.  If you do not guess the number within 7 times, then you lose.
  
'''Guessing Game Algorithm'''
+
'''How to Generate a Random Number'''
* Generate a random number from 0 to 100
 
 
<source lang="python">
 
<source lang="python">
 
import random  # put this line at the very top
 
import random  # put this line at the very top
 
randomNum = random.randint(0, 100)  # generates a random integer between 0 and 100
 
randomNum = random.randint(0, 100)  # generates a random integer between 0 and 100
 
</source>
 
</source>
# Create a variable that keeps track of the number of guesses: '''numberOfGuesses'''
+
 
 +
'''Guessing Game Algorithm'''
 +
# Generate a random number and store it in a variable named '''randomNum'''
 +
# Create and initialize a variable that keeps track of the number of guesses: '''numberOfGuesses''' (What number do you think it should be initialized to?)
 
# Prompt the user to guess a number and store it in a variable: '''guess'''
 
# Prompt the user to guess a number and store it in a variable: '''guess'''
 
# Increase '''numberOfGuesses''' by 1
 
# Increase '''numberOfGuesses''' by 1
 
# If the guess is higher than the random number, then print "Guess lower"
 
# If the guess is higher than the random number, then print "Guess lower"
 
# If the guess is lower than the random number, then print "Guess higher"
 
# If the guess is lower than the random number, then print "Guess higher"
# If the guess is equal to the random number, then print "you win!" and exit loop
+
# If the guess is equal to the random number, then print "You win!" and exit loop
 
## The python command to exit a loop is '''break'''
 
## The python command to exit a loop is '''break'''
# Repeat steps 1 - 4 with loop (while '''numberOfGuesses''' is less than or equal to 7)
+
# If the number of guesses is equal to 7, then print "You lose!" and exit the loop using '''break'''
 +
# Repeat steps 3 - 8 with a loop (while '''numberOfGuesses''' is less than or equal to 7)

Latest revision as of 09:28, 16 December 2016

Objectives:

  • The learner will be able to generate a random number
  • The learner will be able to use if-statements
  • The learner will be able to implement a while loop
  • The learner will be able to nest if-statements inside a while loop

Resources:

Guessing Game Rules

  1. Mr. Bui will think of a number from 0 to 100
  2. You will guess a number
  3. Mr. Bui will tell you to guess higher or lower
  4. You will guess again, and so on and so forth. The above process will only be repeated 7 times. If you do not guess the number within 7 times, then you lose.

How to Generate a Random Number

import random   # put this line at the very top
randomNum = random.randint(0, 100)   # generates a random integer between 0 and 100

Guessing Game Algorithm

  1. Generate a random number and store it in a variable named randomNum
  2. Create and initialize a variable that keeps track of the number of guesses: numberOfGuesses (What number do you think it should be initialized to?)
  3. Prompt the user to guess a number and store it in a variable: guess
  4. Increase numberOfGuesses by 1
  5. If the guess is higher than the random number, then print "Guess lower"
  6. If the guess is lower than the random number, then print "Guess higher"
  7. If the guess is equal to the random number, then print "You win!" and exit loop
    1. The python command to exit a loop is break
  8. If the number of guesses is equal to 7, then print "You lose!" and exit the loop using break
  9. Repeat steps 3 - 8 with a loop (while numberOfGuesses is less than or equal to 7)