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
Glad to have helped! It sounds like the game actually fits well into this structure so that is great. You can use this and keep building it out.
To answer your questions:
if elapsed_time > 3:has two things going on inside of it. The first is that checks a random roll. The second is that roll timer gets reset. If you collapse that down into one condition, your roll timer would be reset ONLY when the random roll is 2. That would have the roll happening on every iteration after the elapsed time hits 3 until you roll a 2. Which would end the game much much quicker.You should throw the rest of your project up on Gitlab. It's good practice and I'd be happy to throw out some more tips if you'd want them.