Search code examples
pythontimer

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.


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(question=="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(question=="bal"):
          print("Balance: "+str(bal))
          time.sleep(5)