Word Game: What is wrong with the following code for the function "play_hand" in PS5. http://pastebin.com/6V0jtKJh I cannot get the final score as the sum all the scores for the words I entered. My output looks something like this: http://pastebin.com/uGYAv1np
You need to take your score assignment out of your display_hand() method. Each time you generate a word, you step into the display_hand() and set 'score=0'. then you evaluate the users input and add scores. Next time through you do this again so 'score' never contains more than the current hand.
Each time you play_hand() (re: line 30), you are resetting your score to zero (re: line 15).
you have a recursive call of play_hand()
in line 23
in line 25 you can use elif instead of if
and another recursive call in line 30
Ok, first of nice work :) The problem isn't: "You need to take your score assignment out of your display_hand() method." or because "You are resetting your score to zero." In line 15. And it's not because you do a rucursive call or because you use "if" instead of "elif". The problem has to do with recursion but arrises because you don't update the score variable. Your play_hand funktion represents a value (ie. it returns a score). When you do the recursive calls, however, you don't use the value. Your score variable updates like this: score=0 score= score + word_score #thats 0+'some value' play_hand() #this is the score of the additional words that #might be made but it isn't added to the score. return score The solution is to update your score variable each time you do a recursive call. (lines 23 and 30.) score=0 score= score + word_score score= score + play_hand() return score I hope that helped, else read this: http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap04.html
@Alfram recursive calls do make a big trouble. You say they don't. Ok, give us your recursive implementation of play_hand() In order to implement a recursive solution to a problem (I prefer to say 'issue' though) there has to be a base case. If you think there is a base case in play_hand(), please, share with us. It is said that play_hand()'s goal is to return a score... (to me it's goal is to play a hand and returning a score is a by-product of its execution, even though a very important one). So, the score, in case of recursive implementation, has to be gotten down to the base case and the function has to then return and there should be as many returns as it were calls, so, the scores have to be added up, right. Anyway, it's up to you. Please, share with us your recursive ideas.
I still think it has to do with 'score' being reset to zero each time play_hand is called. Try this instead: Delete score = 0 then change play_hand(hand, word_list) to play_hand(hand, word_list,score). Set the score to 0 when you call play_hand to actually play the game: play_hand(hand, word_list, 0)
@dbwonders I agree that if one gives it a thought probably the recursive implementation is feasible; however, it requires adding another parameter to play_hand(), namely the 'score' as you mentioned; and the issue, I think is not the feasibility of recursive implementation but that @scorpionismification has not decided on whether to implement play_hand() iteratively or recursively. To me @scorpionismification tried to implement play_hand() iteratively but made two recursive calls unintentionally because recursive implementation would require adding additional parameters to the play_hand() function.
sorry for sort of hijacking this thread @scorpionismification! @vaboro--I understand and agree with what you are saying. Recursion should result in smaller versions of the same problem until it reaches a base case...not solely to create a loop. I was only looking at the problem in terms of how to make it work, not whether or not it was the right solution for the problem. Afraid I wasn't looking at the 'bigger' picture. Thanks for the reminder!
recursion was used for looping before looping constructs were developed how's this - http://pastebin.com/PZEpcQxF i didn't test it
Good work! I think this statment: if word == ".": return score needs to be changed to: if word == ".": return 0 because you have score + play_hand((update_hand(hand,word)),word_list, score) and when function returns after entering '.' no score actually has been added and the previous scores are already in stack; otherwise, it would be double score if it is left as is And by the way raw_input() returns a string by default.
Thank you guys! I went with @dbwonders 's idea.Here's my function: http://pastebin.com/dFnqHzu8 @eSpeX , I think by "take you score assignment out", you meant the same(see link above) @vaboro, I saw your iterative method. I tried it with a hand and it worked except for the condition when all the letters in the hand are used up. i.e. the function doesn't end when we run out of letters. It ends only when we enter a "." Here's what the output of your function looks like: http://pastebin.com/bkTMQaKs Also, is HAND_SIZE a built in function? And what is the meaning of "while word <> '.': " @bwCA, I tried your function, but am afraid, the output doesn't look so good. This is what I got: http://pastebin.com/isiFCUwd i.e. the function doesn't end when we run out of letters, it prints "invalid - choose another word" even after we enter a valid word, and above all, the final out put (after entering "." ) in this case is 63 more than the expected output. @Alfram, I had tried your method but didn't give much time to it and instead went with adding a parameter "score = 0" in the main heading ("don't know a better term for "play_hand()" :)) of my function. However, @bwCA tried to do it; it needs some tweaking though. Finally, In my function (see the first link in this reply), I have used "print score" at two different places and "return None" at three different places. It only works this way. If I try to write "return score" instead, something goes wrong. Can anyone explain why? Thank you all once again for participating. It was a great learning experience! :)
actually, the function ends and the output is here: http://pastebin.com/eniJEn4K it wouldn't end if there were no lines: if letters_left == 0: return I tested it specifically for running out of hand letters HAND_SIZE is a global constant. It is an integer. It is assigned 7 by default as in the assignment but you can give it any value. It cannot be a function because there are no brackets. In order to recognize a global constant they are usually capitalized. Local constants also use lower case. I finished problem set 5 and posted my files. You may have a look at them if you wish. Operator <> is equivalent to != you may look at http://docs.python.org/release/2.5.2/ref/comparisons.html and also http://www.tutorialspoint.com/python/python_basic_operators.htm
@vaboro, thank you for answering my questions. I saw your output. It works fine but, it will work only for hands with 7 letters in them. I had tried with a bigger hand before and that's why it didn't terminate. I added an element counter for different hand sizes and made a minor change in your function accordingly to make it work for hands of all sizes. http://pastebin.com/zarn7Q5b @bwCA, thank you! It works like a charm. :) I however had to add "if count<=0: return score" to make the function terminate when it runs out of letters. http://pastebin.com/
in my function letters_left = HAND_SIZE HAND_SIZE is a global contant, it can be 7, 8, 9, 10.. any number and it is the number of letters in the hand, it is defined globally, so, there's no need in element conter in my implementation if you have a look at my ps5.py file the function works for the HAND_SIZE number of letters in a hand and the number of letters is given; if theh player wants to play with bigger hands, he or she just needs to change the HAND_SIZE constant
"if the player wants to play with bigger hands, he or she just needs to change the HAND_SIZE constant it is " Well in that case, I agree, your function works very well. And a big THANK YOU for explaining me everything in detail! :)
I'm sorry i didn't get back to this before. @vaboro I'm sorry if i offended you by saying that you'r 5 rushed answers were rubbish... I have already explained my recursive ideas :) for your convenience i have added my modifications to the function: ( @scorpionismification ) http://pastebin.com/McYSeWDa Note that this function isn't complete but only the simplest solution to the OP's problem. It still doesn't print the final score, which is returned. To do that you should modify the play_game function. @dbwonders While im at it: a GLOBAL variable is a variable that is accesible whitin any function. It cannot be modified inside a function (unless specifically declared using the global keyword). Any variable that is initialized within the _main_ namespace is global. a LOCAL variable is only accesible within the function in which it's initialized/declared (i don't know the correct term) and can only be modified there. Note that if a global variable is modified within a function, without the use of the global keyword, it's treated as a local variable. This explains why the line 8 "score = 0" isn't resetting the score variable but initializing a variable called score within a new sub-function. Again if this doesn't make sense look at the link i posted above. Section 4.9 and 4.10 has nice pictures.
and ofcourse i forgot something. The function still only ends when you enter a "."
@Now I got the hang of your idea. And it works :) I just made a minor change to make the function end when there are no more letters remaining in the hand. thank u!
@Alfram--Thanks for reminding me to look at your earlier link...I saw one of those nice pictures and I get what you mean now. Each call is a new call. It's not a matter of resetting to zero as it is setting to zero each time. Like you said, the scores were never reset. A pictures worth... If anyone is still reading... Is there any reason not to use the optional parameter, score = 0, in play_hand () (as opposed to score += play_hand((update_hand(hand,word)),word_list)? Just curious ...
some stuff i have been reading about recursion and tail recursion suggests it might make a difference depending on how the compiler works. if you use the score argument and pass the current score to each instance of the function then the function that is currently executing always contains the answer. if the compiler/interpreter recognizes this it won't build up a call stack for all the versions of the function. and the last one just returns the result. the other way, there will be a call stack of all the previous versions of the function waiting for the one above it to return so that it can finish up - this consumes more resources. it may not make any practical difference for what we are doing.
Join our real-time social learning platform and learn together with your friends!