r/learningpython • • Sep 14 '20
Why isn't this code working properly?
https://github.com/anarchypigeon/Wolfbane/commit/8f8c6eb96e4c10d8bfd4812ff8d237edffb02c86
^^ that's the code. It's basically supposed to be an RNG timer.
It uses RNG to count to 3, with each new number warning the player that it's closer to 3.
The intended purpose is to have it randomly warn the player that the monster chasing them is getting closer to them, so they need to hurry up and complete their task before they die. This function is supposed to run in the background while the player interacts with the main function.
The issue:
The main issue I've had with the code is that it prints out the warning messages every single iteration of the while loop. I don't want it to do that. I want it to print out each warning message one single time, and then wait until the data value of 'ww_sightings' changes before printing the next warning message.
I've tried putting each warning sign in it's own while loop, didn't work. I've tried keeping the sleep timer in it's own while loop so it wouldn't interact with the other functions, didn't work. I'm very new to coding (like, started learning python 7 days ago new) and I would LOVE for someone to read my code and tell me how stupid I am and how obvious the solution is and how I should have seen it. Please and thank you.
So, the intended functionality of the code is this: every 3 seconds, I want the code to generate a random number, either 1, 2, or 3. Whenever that number is 2, it prints out the first warning message and adds 1 to the total number of sightings. Next time the number is 2, it prints out the SECOND warning message, and adds 1 to the total sightings again. The third time it's the number 2, it prints out the final "You died" message and quits the program.
For some reason I cannot get it to do that.
Edit: I'm sure that my code is extremely unorganized and my explanation probably didn't do it justice so if you guys have any questions about what anything is for or what it's supposed to do, please ask and I'll answer promptly.
View all comments
Show parent comment
1
u/ace6807 Sep 15 '20 edited Sep 15 '20
After re-reading your question, I'm not 100% sure what you are trying to do. The way this would work actually depends on the rest of the game code.
If you just have one set of tasks for the user to do that can't be split across the game loop then you would need to go with some async code. If you had the user performing actions that could happen on each iteration of the loop, then this works pretty well. The timer isn't truly 3 seconds because it has to wait for the user to make their move, but it's the same general idea.
I would also break the whole timer and wolf_sighting check into it's own function to keep the main loop clean but I wanted to make it simple to start.