Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57938
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-10-29 10:24 -0700 |
| References | (4 earlier) <BhObu.26730$eW3.25642@fx14.am4> <3ab8c365-0985-4035-947c-4d991d9690d7@googlegroups.com> <0qObu.26731$eW3.17586@fx14.am4> <a307611a-c9f7-4c41-856f-564ab7dbb089@googlegroups.com> <azPbu.26732$eW3.9171@fx14.am4> |
| Message-ID | <326cd185-06ba-4c59-b495-c04a9b2dc43b@googlegroups.com> (permalink) |
| Subject | Re: Help with guessing game :D |
| From | rurpy@yahoo.com |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 04:45 -0700
Re: Help with guessing game :D Chris Angelico <rosuav@gmail.com> - 2013-10-29 22:53 +1100
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 04:54 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 05:05 -0700
Re: Help with guessing game :D Alister <alister.ware@ntlworld.com> - 2013-10-29 12:58 +0000
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 06:03 -0700
Re: Help with guessing game :D Alister <alister.ware@ntlworld.com> - 2013-10-29 13:07 +0000
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 06:10 -0700
Re: Help with guessing game :D Alister <alister.ware@ntlworld.com> - 2013-10-29 14:25 +0000
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 07:40 -0700
Re: Help with guessing game :D Alister <alister.ware@ntlworld.com> - 2013-10-29 16:19 +0000
Re: Help with guessing game :D Neil Cerutti <neilc@norwich.edu> - 2013-10-29 17:22 +0000
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 10:32 -0700
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 09:24 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 09:31 -0700
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 09:40 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 10:05 -0700
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 10:13 -0700
Re: Help with guessing game :D rurpy@yahoo.com - 2013-10-29 10:24 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 10:45 -0700
Re: Help with guessing game :D rurpy@yahoo.com - 2013-10-29 11:27 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 11:35 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 12:03 -0700
Re: Help with guessing game :D rurpy@yahoo.com - 2013-10-29 12:55 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 13:08 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 13:21 -0700
Re: Help with guessing game :D rurpy@yahoo.com - 2013-10-29 14:25 -0700
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 10:52 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 10:55 -0700
Re: Help with guessing game :D rurpy@yahoo.com - 2013-10-29 11:26 -0700
Re: Help with guessing game :D rusi <rustompmody@gmail.com> - 2013-10-29 11:32 -0700
Re: Help with guessing game :D Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-29 13:44 +0000
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 06:52 -0700
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 11:05 -0700
Re: Help with guessing game :D Dave Angel <davea@davea.name> - 2013-10-29 19:09 +0000
Re: Help with guessing game :D Robert Gonda <robertgonda1994@gmail.com> - 2013-10-29 12:15 -0700
Re: Help with guessing game :D Dave Angel <davea@davea.name> - 2013-10-30 01:30 +0000
csiph-web