ps5, p4: I am having trouble tallying the total score. http://codepad.org/uOSgVcCJ The problem is that I call the function again, thus erasing any score tallied. Is there a way to avoid this without defining a separate function/adding any variable to play_hand()? Any tips would be much appreciated.
Have you tried to declare score as having a global scope then accessing it locally via global keyword? I had a similar issue with counting numbers of victories/draws/defeats on a Rock-Paper-Scissor game. What I did was creating the function RPSGame as a class instead and declaring score as global. Don't know if this will work on your code, as it seems as this is just a module, right?
My understanding is that you don't have to accumulate scores for multiple hands. Each hand is a new game, starting from scratch, with a score of 0. If you want to keep the score across multiple hands, I'd suggest returning it from the play_hand() function and keep track of it in the play_game() function.
Have you studied loops in python ? May be http://docs.python.org/tutorial/introduction.html#first-steps-towards-programming will help you find solution
BMP: That was going to be my next step, I already use a global variable (HAND_SIZE), so it shouldnt be a problem to add a score global variable dmancine: My problem is that it says a hand is over when the user quits or there are no more letters in a hand, so if they choose to keep going, as I have it now, their total score for the hand is reset with every word they make from that hand. I think the only way to fix it (with my current code) is to have it returned and recorded outside the function as you and BMP suggested
Did reading info. or ones mentioned @ http://docs.python.org/tutorial/introduction.html#first-steps-towards-programming give you any hint?
Not really. Since I call the function again, it will erase/reset 'score' because it resides in the function itself. Using a loop won't change that, unless instead of calling the function I actually code the function - 'score' into a while len(hand) > 0: (or something similar) loop. I was hoping to avoid that. Hopefully this makes sense.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/ps5.py already has while True: , so why change it to an if even though word == '.' is correct as far as the condition to check goes. If you really want to get it working by being able to call the function however many times the user wishes to play , then solutions suggested above could work
Ah. Shriram is on the right track. play_hand() is supposed to let the user input several words for one hand (removing the word from the hand each time). Your implementation only allows a single word. You should be looping in that function to handle multiple words. Then you can keep a local variable outside the loop for the total score.
I think I got it: http://codepad.org/lzCe29F1 I'm sure this isn't the best way to code play_hand, but it seems to work. Thanks for the help Shiram and dmancine!
It looks like quit exits the entire program. It's supposed to return control to play_game(). I don't think you need to go to the extreme of quitting. You're already at the end of execution at the points where you're quitting. Also, the quit after break will have no effect.
Thanks! Wasn't actually clear on how quit/break work but just looked em up.
seanlerner, glad it works now, and you learnt a little more in the process :)
Join our real-time social learning platform and learn together with your friends!