Python Forum
My program won't close and variables won't load correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My program won't close and variables won't load correctly
#2
Your code structure is a mess. You need a main game loop, instead you are using recursion for everything. Let's step through a game:
  • main: I choose 1 to play a new game.
  • start_new_game: Enter 'fred'
  • game_menu_choice: The menu prints.
  • play_the_game: Computer chooses 1. (Good old rock. Nothing beats rock.)
  • computer_choice_rock: I choose 1 to beat rock. (Great minds think alike.)
  • try_again: I choose 3 to quit.
  • write_statistics: My one tie is written to a file. The function returns None. Note that this is the first function to return anything. All the functions above are still active. That's way too much for something as simple as rock paper scissors.
  • try_gain: Comes back in at the end, returns None
  • computer_choice_rock: Comes in at line 134. Then tests the conditional at 135. That's false, so it tests the conditional at 139. That's also false, so the else block on line 144 is executed, telling me I entered an invalid number.
  • computer_choice_rock: This gets called again! Now you're stuck here playing against rock. Admittedly, you could rack up a lot of wins until you hit the recursion limit...

Now, technically, you could fix this by changing the if statements on line 135 and 139 to elif statements, so they only execute if the condition I line 131 is false. But note that you would have to change computer_choice_paper and computer_choice_scissors as well. That is one problem with your code: you are not reusing code, you are duplicating it. The other is that you are using global variables, forcing the individual functions to keep track of the entire program, rather than just what is going on in that function.

Here is what I suggest. Start with one function (*ONE*!) that plays one round of RPS. Use this dictionary:

wins = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
Then you can check wins[bot_choice]. If that is equal to user_choice, the bot wins. If bot_choice equals user_choice then it's a tie. Otherwise, the user wins. That function returns (with the return statement) the user's score (1 for win, -1 for loss, 0 for tie).

Then, once the first function is done, write the second function. The second function calls the first function repeatedly until the user doesn't want to play any more. It gets the user's score from the first function and keeps track of the win/loss/draw statistics. It returns those statistics when it is finished.

Finally, write a third function that can start a new game, load a game, or quit. That one gets the statistics from the first function and writes them to a file (you can also write two other functions: one to save the stats that are passed to it as parameters, and one that loads stats and returns them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: My program won't close and variables won't load correctly - by ichabod801 - Jul-27-2018, 06:51 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020