[Tutor] Guess my number? Guess what's wrong!

Marc Tompkins marc.tompkins at gmail.com
Sat Apr 17 22:25:23 CEST 2010


On Sat, Apr 17, 2010 at 1:04 PM, Matthew Carpenter-Arevalo <
matthew.carpenter.arevalo at googlemail.com> wrote:

> Hi Everyone,
>
> I'm a beginner python programmer, and I've been working on
> the perennial 'guess my number' example.
>
> When I run this in my module, I get an infinite loop of 'higher' or
> 'lower.' Can anyone see where I'm going wrong herE?
>
> I think you just have an indentation problem.  Take a look at your loop.




> Thanks,
>
> MCA
>
>
> # Guess my number
>
> # the computer picks a random number between 1 and 100
> # the player tries to guess it and the computer lets
> # the player know if the guess is too high, too low
> # or right on the money
>
> print "\tWelcome to 'Guess my number'!"
> print "\nI'm think of a number between 1 and 100."
> print "Try to guess it in as few attempts as possible. \n"
>
> # set the initial values
> # the number - represents the number the player has to guess
> # raw_input = the player's first guess & converts it into an integer.
> # tries = # of guesses so far.
>
> import random
>
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess: "))
> tries = 1
>
> # guessing loop
> while (guess != the_number):
>     if (guess > the_number):
>         print "lower..."
>     else:
>         print "Higher..."
>
> guess = int(raw_input("Take a guess: "))
> tries += 1
>
> print "You guessed it! The number was", the_number
> print "and it only took you", tries, "tries!\n"
>
> raw_input("\n\nPress the enter key to exit.")
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100417/37633d03/attachment-0001.html>


More information about the Tutor mailing list