[Tutor] python problem

bhaaluu bhaaluu at gmail.com
Tue Sep 25 13:36:30 CEST 2007


On 9/25/07, Chris <balderas at whtvcable.com> wrote:
> I have this GUESSING GAME code found below:
> ---------------------
> import random
> number = random.randint(1, 101)
> print "I've thought of a number between 1 and 100."
> print "Try and guess it!"
> print
> guess = input( "What's your guess? ")
> while guess != number:
>     if guess > number:
>         print "Your guess is too high."
>     else: #elif guess < number:
>         print "Your guess is too low."
>     guess = input( "What's your next guess? " )
> print "Congratulations! You guessed the number."
> ----------------------------------------------------
> What do I have to do to the code so that the Guessing Game that tries to
> guess a number the user has thought of. (Make sure it can tell if the user
> tries to cheat, e.g. by changing the number during the game??
>
> I hope you can help!
> Chris

I hope so too.
You can have the user enter the "secret number" at the beginning of
the game, and the computer can check it's guess against the secret
number. If the computer's guess is higher than the number, the user
can reply with "<" or "Lower". If the computer's guess is lower than
the secret number, the user can give the computer the hint of ">"
or "Higher", whatever. But since the secret number is stored in a
variable at the beginning of the game, the user can't 'cheat' by
changing the number the computer is trying to guess, in the middle
of the game. Just have a test that checks the computer's guess
against the number stored in the secret number variable.

I recently ran across this problem while working through the Chapter 3
Challenges in Michael Dawson's _Programming Python for the Absolute
Beginner, Second Edition_. Challenge #4 :  Here's a bigger challenge.
Write the pseudocode for a program where the player and the computer
trade places in the number guessing game. That is, the player picks a
random number between 1 and 100 that the computer has to guess.
Before you start, think about how you guess. If all goes well, try coding
the game.

So my question for you, is:
Where is your pseudocode?  The challenge is to plan the game beforehand
using pseudocode. Have you thought about how you guess when you play
the Guess-A-Number game?

Show us your pseudocode for the game.
-- 
bhaaluu at gmail dot com


More information about the Tutor mailing list