# Python guesses MY number

Page 1 of 1

## 7 Replies - 8400 Views - Last Post: 19 December 2010 - 04:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=205719&amp;s=6477d13e6cf7feafb23219a7b69a8069&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ninjastampe

• New D.I.C Head

Reputation: 1
• Posts: 4
• Joined: 16-December 10

# Python guesses MY number

Posted 16 December 2010 - 04:25 PM

Hi there guys. I'm having a problem with an assignment given to me by my current python instructor (that would be my dad). He asked me to make a "reverse guessing game" as in Python being the one guessing your number. I have sort of broken it down into understandability (if that's even a word) for myself and maybe you other guys. This is NOT python code and is ONLY to help you understand it:

```Start of loop
Program asks if it is larger than '(lowest number + highest number) / 2'
If it is;
'lowest number' = '(highest number + lowest number) / 2'
If it is NOT;
'highest number' -= 1

Program asks if it is smaller than '(lowest number + highest number) / 2'
If it is;
'highest number' = '(highest number + lowest number) / 2'
If it is NOT;
'smallest number' += 1
End of loop

```

now, I could write this, but I'm not sure if it's the most efficient way. Dad said, that it had to be the most efficient way of doing it.

Does any of you have an idea that you can break down for me?

(I'd rather code myself, no cheating )

This post has been edited by Dogstopper: 16 December 2010 - 04:26 PM

Is This A Good Question/Topic? 0

## Replies To: Python guesses MY number

### #2 Dogstopper

Reputation: 2956
• Posts: 11,220
• Joined: 15-July 08

## Re: Python guesses MY number

Posted 16 December 2010 - 04:31 PM

I'd keep two pointers. Initially, the low-number pointer is at 0 and the high number of 101 (assuming the range is 1-100).

You can do something like this:
```high = 101
low = 1

guess = (high-low)/2
while guess != number:
if number > guess:
low = guess
elif number < guess:
high = guess
else
print 'Your number is ' + str(guess)
guess = (high-low)/2

```

### #3 ninjastampe

• New D.I.C Head

Reputation: 1
• Posts: 4
• Joined: 16-December 10

## Re: Python guesses MY number

Posted 16 December 2010 - 04:32 PM

Thanks, I'll definately give it a try (in my own coding :

### #4 Dogstopper

Reputation: 2956
• Posts: 11,220
• Joined: 15-July 08

## Re: Python guesses MY number

Posted 16 December 2010 - 04:55 PM

I didn't actually mean to give that to you...I forgot this was a Python post...I use Python as my "pseudo code" in the Java forum

Anyway, hope the logic helps!

### #5 ninjastampe

• New D.I.C Head

Reputation: 1
• Posts: 4
• Joined: 16-December 10

## Re: Python guesses MY number

Posted 16 December 2010 - 05:14 PM

It kinda did and it kinda didn't. Let me show you what I've got so far. When I run this, it just loops between outputting 50 and 75. But I'm going to bed now, and working further tomorrow. Anyways, you can try and run it if you want, see what happens.

```import math
import os
os.system('title Pythons Inexpliccable Guessing Machine')
print "Hello there! Welcome to Pythons inexpliccable guessing machine! Let us start out right away!/n"
skippie=raw_input("If you know how to do this, enter skip ")
if skippie=="skip":
n = 0
while n <> 1:
varmin=1
varmax=100
larger=raw_input("Is your input larger than " + str((varmin+varmax)/2) + " ")
if larger == "yes" or "Yes":
varmin= (varmin+varmax)/2
if larger == "no" or "No":
varmax += 1
smaller=raw_input('Is your number smaller than ' + str((varmin+varmax)/2) + " ")
if smaller == "yes" or "Yes":
varmax=(varmax+varmin)/2
else:
varmin += 1

```

Edited by Dogstopper: !

This post has been edited by Dogstopper: 16 December 2010 - 06:04 PM

### #6 Dogstopper

Reputation: 2956
• Posts: 11,220
• Joined: 15-July 08

## Re: Python guesses MY number

Posted 16 December 2010 - 06:21 PM

I would do several things:
1) Change the loop to a boolean. It's easier to manage.
2) Pull the varmin and varmax OUT of the loop. You DON'T want to reset it every time.
3) Change your interface to accept 1 of 3 possible inputs, not 1 at a time (this simplifies things)
4) if larger or smaller, change the range.

Here is your code with those changes:
```if skippie=="skip":

found = False
varmin=1
varmax=100

guess = (varmax+varmin)/2
ans=raw_input("Is your input larger (l), smaller(s), or equal to (e) than " + str(guess) + " ")
if ans == "L" or ans == "l":
varmin = guess
elif ans == "S" or ans == "s":
varmax = guess
else:
print "Yay! I got it!"
found = True

```

Here is a quote of my session (my number was 63:

Quote

Hello there! Welcome to Pythons inexpliccable guessing machine! Let us start out right away!/n
If you know how to do this, enter skip skip
Is your input larger (l), smaller(s), or equal to (e) than 50 l
Is your input larger (l), smaller(s), or equal to (e) than 75 s
Is your input larger (l), smaller(s), or equal to (e) than 62 l
Is your input larger (l), smaller(s), or equal to (e) than 68 s
Is your input larger (l), smaller(s), or equal to (e) than 65 s
Is your input larger (l), smaller(s), or equal to (e) than 63 e
Yay! I got it!

Finally, it's \n, not /n

### #7 Steven Smith

• New D.I.C Head

Reputation: 6
• Posts: 45
• Joined: 17-March 08

## Re: Python guesses MY number

Posted 17 December 2010 - 12:35 PM

I think it would be appropriate to add entry validation to this as well. In its simplest form, add another elif for E/e, and use the else statement to ask the question again with a reminder to use a correct entry. Otherwise any keystroke other than L/l/S/s registers as a "found answer".

Steve

### #8 ninjastampe

• New D.I.C Head

Reputation: 1
• Posts: 4
• Joined: 16-December 10

## Re: Python guesses MY number

Posted 19 December 2010 - 04:57 PM

Thank you awesomely much guys! I agree with Steve, that might be needed. Also, your thing ALMOST worked! The only thing it doesn't do is be able to guess 100. Try it out yourself, just press "l, l, l, l" it only goes to 99. So I changed varmax to 101, and now it works just fine. Love this site! Thank you guys.