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

Can someone help me understand how my function can return 'None' and still return the value of the return statement as well? If that is confusing and vague I can post my code... thanks.

OpenStudy (anonymous):

Please post your code so we can help you better.

OpenStudy (anonymous):

Normally you wouldn't return None if you were returning a value. If for some reason you wanted to you could use return None, value which would essentially return a Tuple of (None, value). I am guessing that this is probably not what you are wanting so you may want to post code or more of an explanation. Another possibility and the most common reason for a function to return None is that the function is mutating what is sent to it and thus does not have to return anything so it returns None without the use of a return statement. Then you simply use the mutated item that was sent to it.

OpenStudy (anonymous):

I'll just attach my .py file. I know its definitely not the cleanest of most efficient code (i'd love some feedback there on how to improve that as well if you have suggestions) but it works. It just throws back that 'None' right after the 'Here are your available letters: ' portion and after the letters are actually presented. That is where the 'None' is shown. Thanks for your help.

OpenStudy (anonymous):

In the `play_hand` function, right after `print "Here are your available letter:"`, you say `print display_hand(...`. When you want to print a function call in Python, what'll happen is that the return value is printed. So we need to take a look at the return value of `display_hand`. In `display_hand` you print something and then there function stops. When you don't finish a function with a return, Python'll assume that 'None' is returned. In short, what's happening is this: display_hand prints somethings that you actually want to have printed. The function returns 'None', and then that is printed because you wanted the return value to be printed. There're two ways you can fix it: 1) Just call `display_hand` without the print in front of it. 2) Have `display_hand` return a string with everything you want printed, but don't let it print anything by itself.

OpenStudy (anonymous):

I'm glad you asked that question. Mine actually was returning 'None' as well. At the moment, I was still working on completing all the parts of the problem, so decided to just leave it since it wasn't preventing the program from running.

OpenStudy (anonymous):

Very helpful- Thank you slotema. I'll go back and make some changes. On a separate note, (and maybe in the future I should start a new question for this, but for now i'll continue) for question 6a, I can't get the 'get_perm' function to call. I understand what it's doing, but for some reason the moment I call that function Python crashes. Any ideas of what's going on there?

OpenStudy (anonymous):

Are you trying to get get_perms to print out? Because I know with mine I had 7 characters and there were a LOT of perms. (It kept printing for a while.) Try typing this into IDLE just to see if it works: hand = {'e': 1, 'f': 1, 'i': 1, 'h': 1, 'm': 1, 'l': 1} #hit enter n = 6 #hit enter print get_perms(hand, n) #hit enter It should print out a crap ton of combinations of the letters in hand.

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!