Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

Hi, I'm on Hangman in ps2, I can't seem to get my function to return the value which its producing, any idea why that might be? def listcheck(Q, letters): Q=Q.strip().lower() print Q if Q in letters: return Q else: print Q, "Isn't a letter I recognize, please pick a letter from the list provided." print "\n Here are your remaining letters:", letters Q = raw_input("\n Try again:") listcheck(Q, letters)

OpenStudy (anonymous):

Here are the variables: letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] Q = "ld"

OpenStudy (anonymous):

If I understand right, you want to check if Q is in the list of letters and if it is return it, othervise ask user to repeat his input? If that's the case, user will always bee asked to repeat input because Q = 'ld' is not in the list of letters.

OpenStudy (anonymous):

Yeah, the first part of the function checks to see if Q is in the list of letters, but if its not it asks for the user to try entering their input again and reassigns Q accordingly. The function runs to completion, eventually acknowledging the newly added input as being in letters if you enter one of the characters that is. However it returns without including the Q value, despite the fact that I have included "return Q' under the "if Q in letters:" stack. I've checked this by running the function with a print statement above the return. So the function definetly has the correct Q value right before it returns but then doesn't pass any value back. Trying to print Q (I use the same name for the input variable in the larger program) leaves you with a "None" type response.

OpenStudy (anonymous):

Problem is somewhere in input, when I change Q to be one letter, Q = 'g', it works. Will look further.

OpenStudy (anonymous):

So if you enter a single value into the function you will get the right answer, but only because providing the correct answer the first time allows you to bypass the second part of the question, where the problem is. By switching to a while loop from recursion I was able to solve the problem: def listcheck(Q, letters): Q=Q.strip().lower() i=0 while i != 1: if Q in letters: print Q i +=1 else: print Q, "Isn't a letter I recognize, please pick a letter from the list provided." print "\n Here are your remaining letters:", letters Q = raw_input("\n Try again:") print Q return Q Placing the print value after input in the else stack (in the recursive version of the function) makes it clear that its the recursion and not the input which is causing the problem. It still leaves the question of what is causing the recursive version to fail. I think it must have something to do with the way its setup.

OpenStudy (anonymous):

def listcheck(Q, letters): Q=Q.strip().lower() print Q if Q in letters: return Q else: print Q, "Isn't a letter I recognize, please pick a letter from the list provided." print "\n Here are your remaining letters:", letters Q = raw_input("\n Try again:") return listcheck(Q, letters) You missed return when you make recursuve call, see the last line. Cheers

OpenStudy (anonymous):

Aaah, there it is, awesome thankyou!

OpenStudy (anonymous):

It's my plasure.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!