This question has already been answered:

Hello! I want to write an improvement for my game that will be valid for 30 seconds. For this, I used the time module and created a while loop.

import time # timeout variable can be omitted, if you use specific value in the while condition timeout = 300 # [seconds] timeout_start = time.time() while time.time() < timeout_start + timeout: test = 0 if test == 5: break test -= 1 #Пример print('example') 

But this does not suit me, since print ('example') will be executed only upon completion of the while loop. I decided to use such a loop, which also does not suit me because example is displayed on the screen while the while loop is running too often.

 import time # timeout variable can be omitted, if you use specific value in the while condition timeout = 300 # [seconds] timeout_start = time.time() while time.time() < timeout_start + timeout: test = 0 if test == 5: break test -= 1 #Пример print('example') 

Please help me solve this problem. I need the game to work while I use the enhancement.

Reported as a duplicate by jfs , 0xdb , Edward , ߊߚߤߘ , Pavel Durmanov on Feb 26 '18 at 12:50 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • A few not clear question. What does the phrase mean "to keep the game running while using the enhancement."? Your game stops working for the duration of the cycle, but you need to continue? and after 30 seconds performed some action (print ('example'))? - virvaldium
  • The phrase "so that the game continues to work while using the improvement" means that when the player touches the improvement, the game did not stop, that is, the program continued to work as before. The program stops its work during the execution of the cycle, and it is necessary that it should continue for 30 seconds for some action. - Boris Makhlin

1 answer 1

do you just need to print ('example') every second?

if you need one, write time.sleep (1) at the end of the loop. This function stops the whole loop for one second.

  • Thanks, but this is not what I need! - Boris Makhlin
  • What do you need? - Danil Berezovsky