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. -- https://mail.python.org/mailman/listinfo/python-list