### Thread: Help with simple program

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
3
Rep Power
0

#### Help with simple program

Well hello there, I have been asked to make a simple number guessing game in python, however I'm completely lost.
I have to use an import random number function, the number has to be in between 1-1000
As for example, if a computer was to generate a number such as 569 and my first guess would be 165, number 6 will come up as "Y" and other numbers as "N". Keep it as simple as possible, thank you a lot. If possible do use a loop function, thanks again
This is what i got but of course it's completely wrong but you get the idea

import random
number = random.randint(1,9999)
if number ("")
input ("Y")
else ("N")
break
print("You have entered correct number")
2. Congratulations! You wrote a couple lines of code that might be useful in a working program.
Code:
```import random  # useful
number = random.randint(1,9999)  # NO
if number ("")  # NO
input ("Y") # NO
else ("N") # NO
break  # has possibilities.
print("You have entered correct number")  # not great, but OK.```
For your effort I give you 30 lines randomly chosen from a large body of python modules I've written for myself and kept.
Code:
```        pass
finally:
| pipe ...  -- Popen() a shell
else:
)
def integer_polynomial_generator(*args):
b'xi':                  b'u03BE',
if GCD(m,n) == 1:
#                    p = line(distance)
input += key[:max(0,32-len(input)%32)]  # make the input length a multiple of 32
>>> None
import pprint
octagonal_number  = lambda n: n*(3*n-2)
(rows,cols,) = newshape
vec([max(a[i],b[i])for i in range(len(a))]),
import decorator
signal.signal(signal.SIGALRM,self.trap_alarm)
return result()

<BLANKLINE>
def test_me(self):
print(ok)
return'\n'.join(['{']+['%s: %s'%(a,f(getattr(self,a)))for a in dir(self)if a[:2]!='__']+['}'])
a = [0,0]
# curr = cur with possibly repeated
def __ne__(self,other):      return not (self == other)
# -> filename or url, not | etc.
def test_bounding_box(self):
if 3 == c(v[0]):```
3. You should use a kind of "binary search". You can read about it here:

http://codeabbey.com/index/wiki/binary-search

And you can try at first solve similar problem here:

Well... you also need to have some basic skills in Python. Here is the tutorial:

http://docs.python.org/3/tutorial/

And by the way searching through this forum you can find a lot of the posts on the same topic.
4. Binary search won't help as a solution strategy. Binary searches work with ordered lists. There is no ordering here, just "yes the digit is correct" or "not so".

Right on with "basic python skill"!
5. My fault, sorry, I did not read attentively. This game is quite different - something like MasterMind.

Looks I should invent the problem for my site for such game

However here is no check for "same digit but in wrong place"... Or I am inattentive again...

To post author: logic is even simpler - split your secret number into digit list (via "str" perhaps) and the number entered by guesser too. Then in a small internal loop check for the same digits at the same places...
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
3
Rep Power
0
Hey, thank you all for a reply. Sorry to be such a bother but I really have no idea how to do it. I have no problems with c++ and html but only started studying python and i really don't get it. Thanks again. Is there perhaps a site that will tell me how to select specific digit and that will give me output y or n depending on if the number is right? (If that makes sense.)
7. Then perhaps you will write it in C++ and then we could show you how to translate your ideas to python, so the whole thing would be much clearer to you...
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
3
Rep Power
0
Originally Posted by rodiongork
Then perhaps you will write it in C++ and then we could show you how to translate your ideas to python, so the whole thing would be much clearer to you...
That certainly isn't a bad idea, as soon as I get home I will do as you say. Thanks again guys
9. oh this ought to be fun.
Do it! Post your C++ code.
10. Looks I should invent the problem for my site for such game
Ok, I kept promise and tried to create a problem with the same idea. However, it looks like this problem is bit more complicated comparing to original. Since it requires to write a program not for answering but for guessing... Perhaps I'll add simpler variant later.

Code Guesser at codeabbey.com
11. I think, rodiongork, you're over working this problem. The program as I understand it picks 4 digits randomly, (not 5) 0 through 9. And then it scores guesses. Instead of printing digits it displays 'N' or 'Y' if that particular digit is correct. For now let's pretend the range of numbers is 0--9999 inclusive instead of 1-9999 without knowing whether the interval is open or closed.

For instance, if the random number chosen by the program were 1 and your first guess 0 the program should display, because the 0 digits are correct and only the rightmost digit is incorrect,

YYYN

Then your next guess would be 1 and the program would tell you you win!

The guesser can do no better than to start with 0 and increment the digits that are incorrect. The puzzle will always be solved by the tenth guess. The interesting question, which I've graphed but not analytically solved, is

"Given n digits, what is the mean maximum digit?" In other words, for a generalized puzzle what is the average number of guesses?

Hmmm. If the puzzle were binary the answer would involve 2**(-n) as the probability the game takes 2 guesses.
12. Oh, I failed to notice that 'Y' or 'N' should be shown for each particular digits.

Given n digits, what is the mean maximum digit?" In other words, for a generalized puzzle what is the average number of guesses?
Wow!

I agree, it is interesting problem, though I do not know whether it could be beautifully expressed with analytic formula.

As I see, given N digits in K-based numeral system we have:

K^N variants,
of them
(K^N - (K-1)^N) for digit K - 1
((K-1))^N - (K-2)^N) for digit K - 2
...
and 1 for digit 0

This should be multiplied and summed... I'll see whether it could be reduced to some simple formula %)

UPD

Oh, I see, this gives:

(K-1)*K^N - (K-1)^N - (K-2)^N - (K-3)^N - ... - 1
---------------- divided by ------------------
K^N

I fear the tail of the numerator is some well-known formula, but not with any beautiful reduction...