can someone with guessing a number

abhilash pp afilash+python at gmail.com
Thu May 22 08:57:40 EDT 2008


Gary,
  There is a problem in using this sys.exit()
if you are working in windows IDLE, then it will ask you for an 'exit
altogether'

so either change your 'while' clause or restructure your whole program to
fit your idea :-) to avoid failed and success messages together

cheers ,
abhilash

On Wed, May 21, 2008 at 10:35 PM, Dan Upton <upton at virginia.edu> wrote:

> On Wed, May 21, 2008 at 12:55 PM, abhilash pp <afilash+python at gmail.com<afilash%2Bpython at gmail.com>>
> wrote:
> >> On Wed, May 21, 2008 at 10:12 PM, garywood <woodygar at sky.com> wrote:
> >>>
> >>> I would just like the program to exit after guessing the amount of
> >>> numbers wrong
> >>>
> >>> # Guess My Number
> >>> import random
> >>> the_number = random.randrange(100) + 1
> >>> 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
> >>>     if tries > 10:
> >>>         print 'you failed- give up'
> >>>
> >>> 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.")
> >>>
> >>> many Thanks
> >>> --
> >>> http://mail.python.org/mailman/listinfo/python-list
> >>
> >     if tries > 10:
> >         print 'you failed- give up'
> >         break
> > <------------------- use this
> >
>
> That won't work as written, because it'll print "you failed," then
> break, then print "You guessed it!"...
>
> As an alternative to what I suggested before, if you really just want
> to end the program, you could also do
>
> from sys import exit
>
> <rest of code>
>
>    if tries > 10:
>        print 'you failed- give up'
>         exit()
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080522/e02f5c83/attachment-0001.html>


More information about the Python-list mailing list