Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Help with guessing game :D

48 views
Skip to first unread message
Message has been deleted

Chris Angelico

unread,
Oct 29, 2013, 7:53:55ā€ÆAM10/29/13
to pytho...@python.org
On Tue, Oct 29, 2013 at 10:45 PM, Robert Gonda
<robertg...@gmail.com> wrote:
> N = raw_input() #What the user's name is
> print(N + ", I'm thinking of a number between 1-1000") #Not needed but tells the user their name and tells them that it's thinking of a number betweeen 1 to 1000
> guess = input()
> guess = int(guess)

Which version of Python are you using? The raw_input call is very
Python 2, but you're using print as a function, and then you're
converting input()'s result to an integer, both of which suggest
Python 3.x. If you're using Python 2, do NOT use input() - it is
dangerous, deceptively so. In Python 3, that problem no longer
applies.

ChrisA
Message has been deleted
Message has been deleted

Alister

unread,
Oct 29, 2013, 8:58:09ā€ÆAM10/29/13
to
On Tue, 29 Oct 2013 05:05:19 -0700, Robert Gonda wrote:
>>
>> >
>> > converting input()'s result to an integer, both of which suggest
>>
>>

if you need to be checking individual digits you are probably best
keeping the input & number to be checked as strings.

it would then be a trivial task to expand this program to work with words
as well as numbers.




--
"No one gets too old to learn a new way of being stupid."
Message has been deleted

Alister

unread,
Oct 29, 2013, 9:07:08ā€ÆAM10/29/13
to
On Tue, 29 Oct 2013 06:03:55 -0700, Robert Gonda wrote:
> I see, so how should i do it? I wouldn't mind having no text in it I
> just need the program to generate the number and the user to try to
> guess what the number is, so for example if a python would generate num
> 770 and the user would guess 870 it would say NYN

remember that strings are a sequence.
they can be used as iterators & sliced in the same way as lists & tuples.




--
Let a fool hold his tongue and he will pass for a sage.
-- Publilius Syrus
Message has been deleted

Mark Lawrence

unread,
Oct 29, 2013, 9:44:45ā€ÆAM10/29/13
to pytho...@python.org
On 29/10/2013 11:45, Robert Gonda wrote:

As you've already received and responded to advice please could you
read, digest and action this https://wiki.python.org/moin/GoogleGroupsPython

TIA.

--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer

Mark Lawrence

Message has been deleted

Alister

unread,
Oct 29, 2013, 10:25:10ā€ÆAM10/29/13
to
On Tue, 29 Oct 2013 06:10:30 -0700, Robert Gonda wrote:
> Now you have confused me completely, sorry im just new to python and
> just learning everything :) could you perhaps give me an example? or
> part of the code that's missing?

you will probably learn more through trial & error than you will from
being given an answer

to shine some more light on my advise try the following

code="7689"
for digit in code:
print(digit)

does this give you any Ideas on how to proceed?



--
If you're right 90% of the time, why quibble about the remaining 3%?
Message has been deleted

Alister

unread,
Oct 29, 2013, 12:19:31ā€ÆPM10/29/13
to
On Tue, 29 Oct 2013 07:40:20 -0700, Robert Gonda wrote:
>> >>
>> >> remember that strings are a sequence.
>> >> they can be used as iterators & sliced in the same way as lists &
>>
>> >> tuples.
>> >>
>> >> Let a fool hold his tongue and he will pass for a sage.
>>
>>
>> >>
>> >> -- Publilius Syrus
>>
>>
>> >
>> > Now you have confused me completely, sorry im just new to python and
>> > just learning everything :) could you perhaps give me an example? or
>> > part of the code that's missing?

>> you will probably learn more through trial & error than you will from
>> being given an answer
>>
>> to shine some more light on my advise try the following
>>
>> code="7689"
>> for digit in code:
>> print(digit)
>>
>> does this give you any Ideas on how to proceed?
>
> Unfortunately I'm not that sort of person, the way my brain learns is by
> experimenting, but first I need to know exactly what to write. Then I
> will play around with it and customize it to my needs, sorry to be such
> a bother guys :/ and thanks again for your help it's appreciated a lot
> :)

I wont provide the code but i will try to break the problem down into
simple steps (this is the most important skill to develop in programming)


set the number to be guessed
get the user input
step through each digit in the input
compare to the co-responding digit in the number to be guessed
generate the required output

actually let me just expand on my earlier teaser code

does this switch on any light-bulbs for you

data='7865'
guess=input('guess')
for key,digit in enumerate(data):
print digit,guess[key]
--
Watch all-night Donna Reed reruns until your mind resembles oatmeal.

rusi

unread,
Oct 29, 2013, 12:24:57ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 8:10:20 PM UTC+5:30, Robert Gonda wrote:

> Unfortunately I'm not that sort of person, the way my brain learns is by
> experimenting, but first I need to know exactly what to write. Then I will play
> around with it and customize it to my needs, sorry to be such a bother guys :/
> and thanks again for your help it's appreciated a lot :)

D
o
n
't

w
o
r
r
y

R
o
b
e
r
t

In case you did not get the gist of
https://wiki.python.org/moin/GoogleGroupsPython
(which is a it verbose)
your posts come to us like the above.
Just keep a line of context and trim off the rest
a
n
d

y
o
u

s
h
o
u
l
d

b
e

f
i
n
e
Message has been deleted

rusi

unread,
Oct 29, 2013, 12:40:01ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 10:01:38 PM UTC+5:30, Robert Gonda wrote:

> > > I honestly don't get it? this any better? ;D

In google groups you will see a small 'show quoted text'
Click it you will see what a cascading avalanche of mess is produced.


Yes GG is stupid, not you. But if you use it (as many of us here do) you are expected to compensate for that stupidity
Message has been deleted

rusi

unread,
Oct 29, 2013, 1:13:18ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 10:35:52 PM UTC+5:30, Robert Gonda wrote:
> > Is this better then?

By a bit. For most here not enough
Open the 'show quoted text' in your last post it shows like so
[Ive replaced '>' by '&' so GG will show it

& On Tuesday, October 29, 2013 10:01:38 PM UTC+5:30, Robert Gonda wrote:
&
&
&
& > > > I honestly don't get it? this any better? ;D
&
&
&
& In google groups you will see a small 'show quoted text'
&
& Click it you will see what a cascading avalanche of mess is produced.
&
&
&
&
&
& Yes GG is stupid, not you. But if you use it (as many of us here do) you are expected to compensate for that stupidity

Now the actual content and what people expect to see is the following


& On Tuesday, October 29, 2013 10:01:38 PM UTC+5:30, Robert Gonda wrote:
&
& > > > I honestly don't get it? this any better? ;D
&
& In google groups you will see a small 'show quoted text'
& Click it you will see what a cascading avalanche of mess is produced.
& Yes GG is stupid, not you. But if you use it (as many of us here do) you are expected to compensate for that stupidity

Neil Cerutti

unread,
Oct 29, 2013, 1:22:04ā€ÆPM10/29/13
to
On 2013-10-29, Alister <aliste...@ntlworld.com> wrote:
> set the number to be guessed
> get the user input
> step through each digit in the input
> compare to the co-responding digit in the number to be guessed
> generate the required output
>
> actually let me just expand on my earlier teaser code
>
> does this switch on any light-bulbs for you
>
> data='7865'
> guess=input('guess')
> for key,digit in enumerate(data):
> print digit,guess[key]

I just want to add that this programming exercise, while pretty
common, stinks.

A new programmer shouldn't be embroiled in the morass of
interactive programming.

--
Neil Cerutti

ru...@yahoo.com

unread,
Oct 29, 2013, 1:24:08ā€ÆPM10/29/13
to
On 10/29/2013 05:45 AM, Robert Gonda wrote:
> Hey guys, so I figured I will give python a shot. I got to exercise that has asked me to create a number guessing game which weren't a problem,
> guessesTaken = 0 #This is a "Guesses taken counter"
> print("Hello, what's your name?") #Asking the user to input their name
> N = raw_input() #What the user's name is
> import random #This is importing the random function
> number = random.randint(1, 999) #This tells the random function to generate a random number between 1 to 1000
> print(N + ", I'm thinking of a number between 1-1000") #Not needed but tells the user their name and tells them that it's thinking of a number betweeen 1 to 1000
> while guessesTaken < 10:
> print('Take a guess.')
> guess = input()
> guess = int(guess)
> guessesTaken = guessesTaken + 1
> if guess < number: #Says that if the guess is too low it will print a message saying that the guess is too low
> print('Your guess is too low.')
> if guess > number: #Says that if the guess is too high it will print a message saying that the guess is too high
> print('Your guess is too high.')
> if guess == number:
> break #Breaks the loop, meaning it will continue to loop for 10 times while giving them messages from above depending on their results
> if guess == number:
> guessesTaken = str(guessesTaken)
> print("Congrat's, " + N + "! You managed to get the number in " + guessesTaken + " guesses!") #Tells the user they managed to guess it in x number of times
> if guess != number: #If the user is unable to guess the number in 10 times it will stop the loop and give the user a message
> number = str(number)
> print("No, the right number was" + number)
>
> However the problem is that it also asked me to do the following : If at least one of the digit guessed is right it will say "y" otherwise "n" which I can't seem to do :/ any help?

and

On 10/29/2013 08:25 AM, Alister wrote:
> On Tue, 29 Oct 2013 06:10:30 -0700, Robert Gonda wrote:
>[...]
>> Now you have confused me completely, sorry im just new to python and
>> just learning everything :) could you perhaps give me an example? or
>> part of the code that's missing?
>
> you will probably learn more through trial & error than you will from
> being given an answer

While this is true for some people sometimes, I don't think
it is always true. Very often it is easier and faster to
learn something be seeing a worked out example and studying
it to see how it works. This is especially true when one
is new to a programming language and doesn't have a good
understanding of the terminology and concepts that people
who have been using the language take for granted.

> to shine some more light on my advise try the following
>
> code="7689"
> for digit in code:
> print(digit)
>
> does this give you any Ideas on how to proceed?

Robert, please see if this is what you were trying to do:

-------------------------
guessesTaken = 0 #This is a "Guesses taken counter"
print("Hello, what's your name?") #Asking the user to input their name
N = input() #What the user's name is
import random #This is importing the random function
number = random.randint(1, 999) #This tells the random function to generate a random number between 1 to 1000

number_str = str (number) # Convert 'guess' to a string of digits.
while len (number_str) < 3: # If there are less than 3 digits, add leading "0"s until it is three digits.
number_str = "0" + number_str

print(N + ", I'm thinking of a number between 1-1000") #Not needed but tells the user their name and tells them that it's thinking of a number betweeen 1 to 1000
while guessesTaken < 10:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number: #Says that if the guess is too low it will print a message saying that the guess is too low
print('Your guess is too low.')
if guess > number: #Says that if the guess is too high it will print a message saying that the guess is too high
print('Your guess is too high.')
if guess == number:
break #Breaks the loop, meaning it will continue to loop for 10 times while giving them messages from above depending on their results

guess_str = str (guess) # Convert 'guess' to a string of digits.
while len (guess_str) < 3: # If there are less than 3 digits, add leading "0"s until it is three digits.
guess_str = "0" + guess_str
if len (guess_str) > 3: guess_str = guess_str[-2:] # Make sure it is no longer than 3 digits.
# Here, we know that 'number_str' is exactly 3 digits. 'guess_str' is at least
# 3 digits but could be more if the user entered, for example, 34567.
print ("digits matched: ", end='')
for i in range (2, -1, -1):
# 'i' will have the values, 2, 1, 0.
if guess_str[i] == number_str[i]: print ("Y", end='')
else: print ("N", end='')
print()

if guess == number:
guessesTaken = str(guessesTaken)
print("Congrat's, " + N + "! You managed to get the number in " + guessesTaken + " guesses!") #Tells the user they managed to guess it in x number of times
if guess != number: #If the user is unable to guess the number in 10 times it will stop the loop and give the user a message
number = str(number)
print("No, the right number was" + number)
-------------------------

Some comments...
guess_str[-2:]
you want to read about "slices" in the Python docs, for example
http://docs.python.org/2/tutorial/introduction.html#strings
The -2 means indexing starts counting from the right end of the string
rather than the left had the index been positive.

print ("Y", end='')
The end='' means don't print a newline after printing the "Y" string.
See http://docs.python.org/2/library/functions.html#print

Also, what Mark and Rusi were trying to say (not very clearly)
is that when you post from Google Groups, Google Groups insert
a lot of empty lines in the ">" the at the top of the message.

Look at your message,
https://groups.google.com/d/msg/comp.lang.python/6WMfzbtIyi8/AV4sce1zPicJ
(make sure to click the "- show quoted text -" link!)
to see what everybody who doesn't use Google Groups sees.

When post a message, please try to edit you message before
you send it to get rid of those blank lines. In most cases
you can get rid of all the ">" text, *except* for a small
amount that gives the gist of what you are responding to.

rusi

unread,
Oct 29, 2013, 1:32:51ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 10:52:04 PM UTC+5:30, Neil Cerutti wrote:
> I just want to add that this programming exercise, while pretty
> common, stinks.
>
> A new programmer shouldn't be embroiled in the morass of
> interactive programming.

Cheers to that!
If the 'print' statement were called a 'debug' statement, then it would be more clear that a clean and correct program should have no debug (ie print) statements.
Message has been deleted

rusi

unread,
Oct 29, 2013, 1:52:15ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 10:54:08 PM UTC+5:30, ru...@yahoo.com wrote:
> Also, what Mark and Rusi were trying to say (not very clearly)
> is that when you post from Google Groups, Google Groups insert
> a lot of empty lines in the ">" the at the top of the message.

So from the most recent post do you infer that your explanations were successful in creating some understanding?
Message has been deleted
Message has been deleted

ru...@yahoo.com

unread,
Oct 29, 2013, 2:26:28ā€ÆPM10/29/13
to
I have been suitably chastened and will have more respect for a variety
of approaches in the future.

ru...@yahoo.com

unread,
Oct 29, 2013, 2:27:41ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 11:45:56 AM UTC-6, Robert Gonda wrote:
> Thank you very much for your reply, however it gives me an error,
> something about the "end", do you know whats wrong with it?
> (Still not sure if im posting this right so sorry)

"...an error, something about the 'end'" is not much to go on. :-)

Most of the time, when there is an error in a python program,
Python will print "traceback" error message. When asking for
help please copy and paste those lines in your post. Without
that it is just a guessing game for anyone to try and figure
out what is wrong.

Did perhaps your traceback message look like this?

File "xx3.py", line 28
print ("digits matched: ", end='')
^

If so, you are running your program with python2, not python3.
So you need to either figure out how to run python3 (does entering
the command "python3" do anything?) or change the program to work
with python2.

If the error message was different than above, you need to post
it here if people are to have any chance of helping you figure
out what is wrong.

rusi

unread,
Oct 29, 2013, 2:32:39ā€ÆPM10/29/13
to
Heh! Dont be hard on yourself!
When youve been a teacher long enough you will know
- communication is the exception*
- misunderstanding is quite ok
- non-understanding is the norm

* Yeah Fr. Thomas Keating talks about the fact that he prefers communion to communication. Hopefully when I go to heaven I'll find out how the admin there is so efficient
Message has been deleted
Message has been deleted

Dave Angel

unread,
Oct 29, 2013, 3:09:01ā€ÆPM10/29/13
to pytho...@python.org
On 29/10/2013 14:05, Robert Gonda wrote:


& >> Back to question, name is also not working, I currently have
python 3.3.2 and the only to get that work is the write raw_input, I
have no idea why, did i do soemthing wrong?


Why did you add those two >> symbols in front of your new text? Each
such symbol is supposed to indicate that another level of quoting is
occuring. So when I saw your message, I first concluded that you sent a
blank reply.

(I also added another character in front of it, so you'd see it.
Apparently googlegroups messes up your view of things in order to cover
up its bugs in posting.

As for your question. Yes, you did something wrong. You thoroughly
underspecified the phrase "not working."

When you run a program and it gets an exception, read the whole
traceback. And when you want help here, copy the WHOLE TRACEBACk.

If raw_input() is working without an exception, then you are NOT running
Python3.x. Figure that out first, perhaps by sticking these two lines
at the beginning of your code:

import sys
print(sys.version)



--
DaveA


Message has been deleted

ru...@yahoo.com

unread,
Oct 29, 2013, 3:55:13ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 1:03:00 PM UTC-6, Robert Gonda wrote:
> never mind you was right, for some reason I had version 2.7 :/ ,
> and btw I was wondering, is it also possible to make it more
> complex? such as if the computer will again show ā€œYā€ if a digit
> is correct but if a digit is incorrect it will say "H" as in
> too high or ā€œLā€ if it's too low? (while still keeping "Y").
> Do tell me if it sounds confusing :/

Sure it's possible. What do you think would happen if you
replaced the code that compares the digits and prints Y or
N with with something like this?

if guess_str[i] > number_str[i]: print ("H", end='')
if guess_str[i] < number_str[i]: print ("L", end='')
if guess_str[i] == number_str[i]: print ("Y", end='')

(you are comparing 1-character long strings containing a
digit between "0" and "9" but they will compare the same
way numbers do.)
Message has been deleted
Message has been deleted

ru...@yahoo.com

unread,
Oct 29, 2013, 5:25:34ā€ÆPM10/29/13
to
On Tuesday, October 29, 2013 2:21:08 PM UTC-6, Robert Gonda wrote:
> Is it possible to further more specify it? H only shows if the
> guess is at most 3 higher then the answer?. But L is only given
> if the guess is at most 3 lower the answer? I'm starting to
> like this ;D

To do that, you'll need to convert the string digits back to
numbers that you can do arithmetic on. The int() function
can do that. Then you can do something like

if guess_num > number_num + 3: ... print what you want here.

You'll find you get more and better answers to your questions
if you attempt to do something yourself and when you find it is
not doing what you want, post here saying what you tried, what
it did, and how what it did is different from what you want.

You'll also get better responses if you edit out the empty
and excess ">" lines in the quoted text of your replies,
which you are still not doing.

Dave Angel

unread,
Oct 29, 2013, 9:30:40ā€ÆPM10/29/13
to pytho...@python.org
On 29/10/2013 15:15, Robert Gonda wrote:

(once again deleting all the double-spaced Googlegroups nonsense)

>
&& >>Hi dave, yes you was right. I had python 2.7 but I upgraded to
python 3 now, thanks for help :) by the way, is this showing normally?

No, you're still adding a ">" character before the stuff you typed.
That's supposed to show what you are quoting. One bracket is the person
you're replying to, two marks the stuff he was quoting, and so on.

Any decent mail program does it for you automatically, or with minimum
configuration.

Only googlegroups messes it up so consistently.

--
DaveA


0 new messages