Help with guessing game :D

R

Robert Gonda

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?
 
C

Chris Angelico

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
 
R

Robert Gonda

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

Hi Chris and thanks for the reply, I'm using python 3.
 
R

Robert Gonda

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.

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 userwould guess 870 it would say NYN
 
R

Robert Gonda

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?
 
R

Robert Gonda

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 :)
 
R

rusi

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
 
R

rusi

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
 
R

Robert Gonda

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
 
R

rusi

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
 
N

Neil Cerutti

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.
 
R

rurpy

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

]
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 == number_str: 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.
 
R

rusi

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.
 
R

Robert Gonda

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



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


for digit in code:


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 == number_str: 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.
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)
 
R

rusi

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?
 
R

Robert Gonda

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Guess the Number Repeat 1
Help in hangman game 1
vhdl guessing game 2
Python Unit Tests 10
Python battle game help 2
Tic Tac Toe Game 1
guessthenumber print games left 14
Guessing the encoding from a BOM 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top