Write a program that plays the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The pro- gram then displays

I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
The player then types a first guess. The program responds with one of the following:
1. Excellent! You guessed the number!
Would you like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.
If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player “zero in” on the correct answer. After a game ends, the program should prompt the user to enter "y" to play again or "n" to exit the game.


```
# Guess the number game.

import random

choice = "y" # holds user choice

while choice != "n":

# if a new game prompt is displayed
if choice == "y":
secretNumber = random.randrange( 1, 1001 )
print "I have a number between 1 and 1000."
print "Can you guess my number?"
print "Please type your first guess."

# alters choice so prompt displays only once per game
choice = " "
elif choice != " " and choice != "n" and choice != "y":
print "Invalid choice. Enter y to play again, n to quit."
choice = raw_input( ">" )
continue

# user guess
guess = int( raw_input( ">" ) )

# processes the user guess, displaying hints
if guess > secretNumber:
print "Too high. Try again."
elif guess < secretNumber:
print "Too low. Try again."
elif guess == secretNumber:
print "Excellent! You guessed the number!"
print "Would you like to play again (y or n)?"
choice = raw_input( ">" )
```
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
>500
Too high. Try again.
>300
Too low. Try again.
>400
Too low. Try again.
>450
Too high. Try again.
>425
Excellent! You guessed the number!
Would you like to play again (y or n)?
>n

Computer Science & Information Technology

You might also like to view...

Write a prototype and prototype comments for the sqrt library function.

What will be an ideal response?

Computer Science & Information Technology

To print speaker notes, first click the PRINT tab.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

A ________ allows you to add, edit, delete, or view data, usually one record at a time

A) macro B) field C) form D) table

Computer Science & Information Technology

The ____ deallocates the memory occupied by the nodes of a list when the class object goes out of scope.

A. constructor B. destructor C. head pointer D. tail pointer

Computer Science & Information Technology