[Tutor] High Low Game

Alan Gauld alan.gauld at btinternet.com
Tue Oct 19 01:37:47 CEST 2010


"Raquel" <raquel.adams at gmail.com> wrote

> I am new to Python, and have hit a WALL.  Any help is appreciated! 
> Below
> is what I have so far, for the "High Low Game"...when I try to run 
> it,
> nothing happens, so I have been unable to finish it.  I am sure it 
> is
> something simple...

First some questions:
1) What OS are you using?
2) What version of Python
3) How are you trying to run the program?
4) When you say "nothing happensd" is that absolutely true?
    Is anything displayed, even fleetingly?

Second, some comments on your code:

> def main():
>     print " Pick a number between 1 and 1000 and I will try to guess 
> it"
>     print "in no more than 10 tries. After each guess, enter 0 if I"
>     print "got it right, -1 if I need to guess lower, and 1 if I 
> need to"
>     print "guess higher."

You could do this with a single print statement and a triple quoted 
string.
And if you made it a variable you wouldn't even need a function,
just print the prompt message:

prompt = """
Pick a number between 1 and 1000 and I will try to guess it
in no more than 10 tries. After each guess, enter 0 if I got it right,
-1 if I need to guess lower, and 1 if I need to guess higher.
"""

print prompt

> high=1000
> low=1
> tries=1
>
> while high > low:
>     ave=(high+low)/2
>     print "My guess is", ave,

Your guesses will always be the same since you never change
high or low... For the same reason you will never exit the while loop.

>     guess=int(raw_input("Please enter '-1','0',or '1'):"))
>     print guess
>     if guess == 0:
>          print "That took 1", tries, "guesses."

I don't think you want the 1 in there. tries should suffice?
Also you might want to force an exit from the while loop here?

>     elif guess == -1:
>          print "I will guess lower."
>     elif guess == 1:
>          print "I will guess higher."

Nice promise but you only ever guess lower.
You need to move the guess creation code into the appropriate
if/else block.

>     else:
>          print "Pick a number between 1 and 1000 and I will try to 
> guess
> it"
>          print "in no more than 10 tries.  After each guess, enter 0 
> if I"
>          print "got it right, -1 if I need to guess lower, and 1 if 
> I need
> to"
>          print "guess higher."

You don't need this since you print it in the next line anyway!

>     main()

This could become the "print prompt" line...

While there are flaws in the logic it looks like it should work after
a fashion, aso that brings me back to the question of how you
are running it?.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list