[Tutor] Fwd: Re: Simple guessing game - need help with the math

Greg Markham greg.markham at gmail.com
Wed Aug 13 16:19:22 CEST 2014


My apologies, this reply didn't make its way to the list.

>
> On Aug 13, 2014 1:25 AM, "diliup gabadamudalige" <diliupg at gmail.com>
wrote:
> >
> > Wouldn't it be easier to work it this way?
> > for example:
> > number range =100
> > your selection = 82
> >
> > round 1
> > computer guesses 50
> > you say higher
> > then in round 2 the computer makes a guess only between 51 and 100
> > computer guesses 92
> > you say lower
> > round 3 computer guesses between 51 and 91
> > round 4 computer guesses 75
> > you say higher
> > now guess is between 76 and 91
> > etc. till the correct answer is reached
> > you can use the random and range to select a random number between two
numbers
>
> Interesting.  I hadn't thought of that. And, it appears that would
circumvent the issue I'm running into with rounding. I will try that
approach; thank-you.
>
> But my curiosity is still begging me for an answer regarding my original
approach. Is there a way to manage the functionality of be round function
such that it does not strip any data to the right of the decimal point?
>
> Thanks,
>
> Greg
>
> >
> >
> >
> > On Wed, Aug 13, 2014 at 4:55 AM, Greg Markham <greg.markham at gmail.com>
wrote:
> >>
> >> Hello again,
> >>
> >> I'm working on one of the more advanced (at this stage of my learning
process), albeit optional, assignments.  The objective is to write a
program that tries to guess your number from 1 to 100.  After each guess,
you tell it if it's (h)igher, (l)ower or (c)orrect, and eventually it'll
logically zero in on the correct value.  In addition, it should count the
number of tries required to come to the correct guess.
> >>
> >> I've worked through a few hiccups so far, but I've come to a
standstill.  Here follows my code:
> >>
> >> import os
> >>
> >> print ("\nShall we play a game?\n\n")
> >> print ("Think of a number, 1 to 100, and I will attempt to guess what
it is.\n")
> >>
> >> input("Press [Enter] when ready.\n")
> >>
> >> guess = 50
> >> change = 50
> >> answer = ""
> >> tries = 1
> >>
> >> while answer == "h" or "l" or "c":
> >>     print ("My guess is: ", guess, "\n")
> >>     answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
> >>     answer = answer.lower()
> >>     if answer == "h":
> >>         guess = round(int(guess + (change/2)))
> >>         change = change/2
> >>         tries += 1
> >>     elif answer == "l":
> >>         guess = round(int(guess - (guess/2)))
> >>         tries += 1
> >>     elif answer == "c":
> >>         print ("/n/nYay!  I win.  Shall we play again?\n\n")
> >>         os.system('cls' if os.name == 'nt' else 'clear')
> >>     else:
> >>         print ("Invalid response.  Please try again.\n")
> >>
> >>
> >> Something about the math in the code here isn't quite right.  If you
were to enter "h" (for 'higher') each time, it would first guess 50, then
75, then 87, then 93, then 96, and finally would stop progressing at the
number 97.  I suspect it's a side-effect of the way the round function
works, but I don't know how to get around it.
> >>
> >> Any help would be greatly appreciated.
> >>
> >> Thanks in advance,
> >>
> >> Greg
> >>
> >> _______________________________________________
> >> Tutor maillist  -  Tutor at python.org
> >> To unsubscribe or change subscription options:
> >> https://mail.python.org/mailman/listinfo/tutor
> >>
> >
> >
> >
> > --
> > Diliup Gabadamudalige
> >
> > http://www.diliupg.com
> > http://soft.diliupg.com/
> >
> >
**********************************************************************************************
> > This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
> >
**********************************************************************************************
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140813/0d7e6e4d/attachment.html>


More information about the Tutor mailing list