Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to disable only one thing (from user input) and keep the others running for 30 seconds?

Let’s say you are making a game and it has an input that asks you what do you want to do.

 bal=0
while True:
  question=input("What do you want to do?")
    if(input=="beg"):
      print(Adding 500$ to your balance.)
      bal=bal+500
    elif(input=="bal"):
      print("Balance: "+str(bal))

I want to make a timer so the user cannot use beg command for 30 seconds. But still be able to use other commands -I’ll probably add more commands.
Btw I couldnt do indents here the website page went downward but imagine the while true loop and ifs has correct indent.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do it with the help of time module.

import time
bal=0
start = 0
while True:
  question=input("What do you want to do?")
    if(input=="beg"):
      if(time.time()-start>=30):
          print(Adding 500$ to your balance.)
          bal=bal+500
          start = time.time()
      else:
          print("Wait for",30-time.time()+start,"seconds")
    elif(input=="bal"):
      print("Balance: "+str(bal))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading