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

2014-08-13 Thread Greg Markham
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise to work on

2014-08-13 Thread Joseph Lee

Hi Keith,
As a follow-up: I'm sure others might have mentioned this, but I 
also recommend doing exercises presented in the book or reference 
you're using to learn Python.

Cheers,
Joseph

- Original Message -
From: keith papa Hi, am a newbie to python and I wondering if you guys can give me 
some exercise to work on.  I have learned: print function , if 
function , strings, variables, and raw_input.  The more the 
better

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise to work on

2014-08-13 Thread Joseph Lee

Hi Keith,
It depends on what you want to do with Python in the end (by the 
way, if statement is not a function).


Here are some of my suggestions (based on current topics and 
things to come later):


1.  Write a loop which prints even numbers from 1 to 100.

2.  Write a program which determines if the entered number is an 
even or odd number.


3.  Given a string, locate the last occurrence of a character.

4.  Write a program that says number of characters in a string 
and whether it starts with a consonant or a vowel.


I think that's it for now.  Again the rest of the exercises that 
might show up might depend on your ultimate goal with Python (by 
the way, which version are you using?).  Other topics to learn 
about would be how to write functions, reading from a file, while 
and for loops and working with organized data such as lists and 
dictionaries.


Cheers,
Joseph
- Original Message -
From: keith papa Hi, am a newbie to python and I wondering if you guys can give me 
some exercise to work on.  I have learned: print function , if 
function , strings, variables, and raw_input.  The more the 
better

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise to work on

2014-08-13 Thread Greg Markham
Keith,

This should get you started...

http://learnpythonthehardway.org/book/

http://www.codecademy.com/en/tracks/python

http://docs.python-guide.org/en/latest/intro/learning/

Happy coding!

--Greg


On Tue, Aug 12, 2014 at 1:52 PM, keith papa  wrote:

> Hi, am a newbie to python and I wondering if you guys can give me some
> exercise to work on. I have learned: print function , if function ,
> strings, variables, and raw_input. The more the better
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2014-08-13 Thread Greg Markham
My apologies, this reply didn't make its way to the list.

>
> On Aug 13, 2014 1:25 AM, "diliup gabadamudalige" 
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 
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@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.
> >
**
> >
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2014-08-13 Thread Dave Angel
Greg Markham  Wrote in message:

Please post as text. Because you used html, I cannot quote your
 message,  or indeed even see it at all while replying. There are
 other problems frequently triggered by html, but on to my
 response.

You don't specify the python version you're writing for.  I have
 to guess 3.x, since your use of input would have been improper in
 2.x.

Your immediate problem is indeed caused by truncation.  In python
 version 2.x, dividing an int ny 2 will truncate down.   You can
 fix that by using
change = int ((1 + change)/ 2)

You also need to repeat that in the appropriate elif clause.

You do have other problems,  however,  Your calculation for the
 new guess is wrong in the elif clause.  See if you can spot the
 problem.  Your while loop termination condition will end the loop
 if a user types something other than one of the three valid ones.
 And your logic when the user says 'c' starts the next game
 without reinitializing guess to 50.

Your use of round in the guess= lines is superfluous.  But you may
 need to add 1 here as well. I'd suggest starting change at 100,
 and cutting it in half before adding or subtracting it from the
 guess. 

Incidentally,  once your code is fixed,  it'll converge much
 faster than the other random suggestion. Further,  the binary
 search technique is well worth understanding and mastering.
 

If I were coding this,  then instead of keeping a 'change'
 variable, I'd keep an upperlimit and a lowerlimit one. Start them
 at 0 and 101, and start your loop. Each time through the loop
 your guess would be halfway between the limits. And each time the
 user tells you that you were high or low, you'd adjust the
 upperlimit or lowerlimit respectively. 


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor