Difference between revisions of "Guessing Game Assignment"
From WLCS
Line 9: | Line 9: | ||
* Generate a random number from 0 to 100 | * Generate a random number from 0 to 100 | ||
<source lang="python"> | <source lang="python"> | ||
− | import random | + | import random # put this line at the very top |
− | randomNum = random.randint(0, 100) | + | randomNum = random.randint(0, 100) # generates a random integer between 0 and 100 |
</source> | </source> | ||
# Prompt the user to guess a number | # Prompt the user to guess a number | ||
Line 16: | Line 16: | ||
# If the guessed number is lower than the random number, then print "Guess higher" | # If the guessed number is lower than the random number, then print "Guess higher" | ||
# If the guessed number is equal to the random number, then print "you win!" and exit loop | # If the guessed number 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 the guessed number does not equal the random number) | # Repeat steps 1 - 4 with loop (while the guessed number does not equal the random number) |
Revision as of 13:40, 25 October 2008
Guessing Game Rules
- Mr. Bui will think of a number from 0 to 100
- You will guess a number
- Mr. Bui will tell you to guess higher or lower
- You will guess again, and so on and so forth.
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
- Prompt the user to guess a number
- If the guessed number is higher than the random number, then print "Guess lower"
- If the guessed number is lower than the random number, then print "Guess higher"
- If the guessed number 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 the guessed number does not equal the random number)