3

I'm an amateur coder. I'm working on a small little game for a project in biology, but I have come across an issue in my code. I have a loop that adds +1 to the variable sunlight every two seconds. However, all code below the loop is non-functional now that I have made the loop. I'm guessing it's because it's waiting for the loop to finish. Any way to have the loop always run but allow the code to run through it's sequence at the same time?

print("Game started!")
sunlight = 0
while True:
  time.sleep(2)
  sunlight += 1
  commands = input("Type stats to see which molecules you have, type carbon to get carbon\ndioxide, and type water to get water: ")
  if commands == ("stats"):
    print("Sunlight: ",sunlight,"")
1
  • 1
    You need a condition in your while loop that eventually becomes false so the rest of the code can run Commented Nov 22, 2019 at 18:40

3 Answers 3

2

As you are beginner, i would not recommend to use multithreading or asyncio. Instead just start the time and when user enter "stats", elapsed time//2 will be equal to sunlight.

import time
start_time = time.time()
while True:
    commands = input("Type stats to see which molecules you have, type carbon to get carbon\ndioxide, and type water to get water: ")
    if commands == ("stats"):
        sunlight = (time.time()-start_time)//2   # elapsed time // 2
        print("Sunlight: ", sunlight, "")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help! It works just fine now!
1

Your sunlight variable basically functions as a clock; it counts half of the number of seconds since the program begins. Rather than implement your own clock using time.sleep(), it's better to just use an existing clock from the time library.

The function time.monotonic returns a number of seconds, so you can use this to get the current sunlight by saving the start time, then each time you want to know the value of sunlight, take the difference between the current time and the start time, divided by 2.

start_time = time.monotonic()

def get_sunlight():
    current_time = time.monotonic()
    return int(current_time - start_time) // 2

It is better to use the monotonic() function than the clock() function for this purpose, since the clock() function is deprecated as of Python 3.3:

The time.clock() function is deprecated because it is not portable: it behaves differently depending on the operating system.

It's also better than the time() function for this purpose, because changes to the system clock (such as going forwards or back due to daylight savings time) will affect the result of time():

While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

Comments

-1

You should look into the multithreading library. That's probably a good resource. You can fire off a thread running your sunlight incrementer that updates a global variable (not a good idea but you seem to have just 1 writer, so you can get by till you have time to pick up more advanced parallel processing concepts).

Reference: https://www.geeksforgeeks.org/multithreading-python-set-1/

5 Comments

i'd look at asyncio instead
True, asyncio is another possibility. It just seemed like it's kinda modular so multithreading was the first thing that popped into my head :)
multithreading is both incredibly hard, and at the same time rather pointless in python because of the GIL.
It isn't that hard tbh, and a program like this doesn't exactly need "true" multithreading.
Have an upvote, a lot of people stick with what they know rather than the right tool for the job. Multithreading as you said isn't THAT hard and for some applications is more appropriate than parallel.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.