[Tutor] Simple game

Alan Gauld alan.gauld at btinternet.com
Thu Mar 24 21:26:45 EDT 2016


On 24/03/16 18:12, Tom Maher wrote:

> raw_input('your input ')

Raw input returns a value, you need to store it somewhere like:

value = raw_input('your input ')

> while raw_input != 'c' or 'h' or 'l':

Then you need to compare vale not the function name.
(Did you learn BASIC at some point maybe?)

You need to read/think like the computer not like a human.
Python sees that while like

while value != ('c' or 'h' or 'l'):

In other words it evaluates 'c' or 'h' or 'l' first
and that will always be True. It then compares your
input value to True and if you entered any kind of
value (not an empty string) then value will be True
and your while will never run. (I'm pretty sure
the != is wrong too, you really want ==)

A better way to code this specific kind of test is
to use the in operator:

while value in ('c','h','l'):

>       if raw_input == 'h':
>            guess += 1
>            n -= (n/2)


Again you need to use value

if value == 'h':....

Finally you need to print the new value of the computers
guess inside the loop or the user can never tell which way to go.
You then need to read a new command inside the loop too.

So it should probably look something like:

initialise values here
while True:
    print "My guess: , n
    value = raw_input....
    if value not in ('h','c','l'):
       print "must enter hl or c"
       continue
    if value == 'h':...
    elif value == 'l'...
    else:
       print 'success in ' , guess, 'guesses'
       break

All untested so you will need to tweak it a bit.
But hopefully it points you in the right direction.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list