Difference between revisions of "Guessing Game Assignment"

From WLCS
Line 3: Line 3:
 
# You will guess a number
 
# You will guess a number
 
# Mr. Bui will tell you to guess higher or lower
 
# Mr. Bui will tell you to guess higher or lower
# You will guess again, and so on and so forth.
+
# 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'''
 
'''Guessing Game Algorithm'''
Line 11: Line 11:
 
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>
# Prompt the user to guess a number
+
# Create a variable that keeps track of the number of guesses: '''numberOfGuesses'''
# If the guessed number is higher than the random number, then print "Guess lower"
+
# Prompt the user to guess a number and store it in a variable: '''guess'''
# If the guessed number is lower than the random number, then print "Guess higher"
+
# Increase '''numberOfGuesses''' by 1
# If the guessed number is equal to the random number, then print "you win!" and exit loop
+
# If the guess is higher than the random number, then print "Guess lower"
## The python command to exit a loop is ''break''
+
# If the guess is lower than the random number, then print "Guess higher"
# Repeat steps 1 - 4 with loop (while the guessed number does not equal the random number)
+
# 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'''
 +
# Repeat steps 1 - 4 with loop (while '''numberOfGuesses''' is less than or equal to 7)

Revision as of 07:58, 11 October 2011

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.

Guessing Game Algorithm

  • Generate a random number from 0 to 100
import random   # put this line at the very top
randomNum = random.randint(0, 100)   # generates a random integer between 0 and 100
  1. Create a variable that keeps track of the number of guesses: numberOfGuesses
  2. Prompt the user to guess a number and store it in a variable: guess
  3. Increase numberOfGuesses by 1
  4. If the guess is higher than the random number, then print "Guess lower"
  5. If the guess is lower than the random number, then print "Guess higher"
  6. 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
  7. Repeat steps 1 - 4 with loop (while numberOfGuesses is less than or equal to 7)