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 do i have the if statement become effective after the 30 seconds

I want the if statement working after the 30 seconds but that isn’t the case right now. I heard people recommend threading but that’s just way too complicated for me.

import os
import time

print('your computer will be shutdown if you dont play my game or if you lose it')

shutdown = input("What is 12 times 13? you have 30 seconds.")

time.sleep(30)

if shutdown == '156':
    exit()

elif shutdown == '':
    print('you didnt even try') and os.system("shutdown /s /t 1")

else:
    os.system("shutdown /s /t 1")

I tried threading already but that is really complicated and I’m expecting to print you didn’t even try and shutdown after the 30 seconds if you didn’t input anything

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 :

I recommend to use threads because it makes the thing much easier here. Try this:

import threading
import time

user_input = ""
ANSWER_TIME = 30

def time_over():
    match input:
        case '156':
            exit(0)
        case '':
            print('you didnt even try')
            os.system("shutdown /s /t 1")
        case _:
            os.system("shutdown /s /t 1")

exit_timer = threading.Timer(ANSWER_TIME, time_over)
print('your computer will be shutdown if you dont play my game or if you lose it')
exit_timer.start()

user_input = input("What is 12 times 13? you have 30 seconds.")

Note that I replaced the if-else statements with match-cases, which are IMHO more readable. I also replaced your and statement (if you want to execute two statements, just write them below each other).

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