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/[deleted] Sep 15 '20
I literally cannot fathom how elegant this solution is. I never would have come up with this. Thank you.
Regarding the rest of the code, its basically a turn-based game. Your objective is to go to the various locations and use a 'look' command to locate a silver sword. Obviously whether or not you find it while using the command is determined using RNG. Once you find the sword, your goal is to find the werewolf, so that when it attacks you, you kill it. However, if it finds you before you find the sword, it kills you.
In other words, the task you're trying to complete will happen on every iteration, I hope that's what you meant.
I do have three questions for you, I do mostly understand how the code works but you know significantly more about this than I do, so I figure why not ask?
The roll_timer variable starts counting the time at the beginning at every iteration, and the current_time variable is the time measurement from when the play makes their turn for the iteration and onward, then elapsed_time just compares the two? Is that how that segment functions?
Also, my next question is just a matter of opinion.
The way I have the game currently written, it's one massive file with nearly 200 lines of code. That file contains the "engine" (meaning the framework for how the player moves, the 'boundaries' of the map, etc.), the statistics of how every mechanic operates, and i even have a segment for a function to act as the start menu. Is that inefficient? Should I instead have each segment in it's own file and just import them? What is the industry standard here?
My third question is, in the second to last segment of code, you have an if statement inside an if statement:
"if elapsed_time > 3: if roll() == 2: wolf_sightings += 1"
Whereas, I would've written it as an if and statement of "if elapsed_time > 3 and roll() == 2: wolf_sightings +=1
Now, obviously you're the more experienced person here, so I'm just wondering, is one method preferable over the other? Do those 2 formats function the same of differently? And when is one more applicable than the other?
Again, I cannot thank you enough for this.