I'm a beginner in Python (and in programming in general) and as a small test for myself I'd made a program that acts a bit like the lock-screen on your phone. I still needed strike to go back down over time and looked for some ways to run multiple while loops at the same time. I decided to use Process, but now I get errors every time I try to run the program.
Program:
import time
from multiprocessing import Process
Password = 20051402 # password of your choice
strike = 0 # defines strike
password = None # defines password "at the module level"
def loop_redemption(): # decreases strike over time
global strike
while True:
while strike >= 1:
time.sleep(5)
strike -= 1
def loop_main():
global strike
while True:
if strike >= 3: # pauses for 5 seconds while counting down
for time_passed in range(5):
print("Timed-out for " + str(5 - time_passed) + " seconds.")
time.sleep(1)
global password
password = int(input("Password: "))
if password == Password:
Process(target=loop_redemption).terminate()
break # ends loop when correct password is entered
else:
strike += 1 # gives you a strike everytime you get the password wrong
if strike == 1:
print("Two attempts remaining before time-out.")
if strike == 2:
print("One attempt remaining before time-out.")
if __name__ == '__main__': # allows both while-loops to run simultaneously?
Process(target=loop_redemption).start()
Process(target=loop_main).start()
if password == Password:
print("Password correct. Enjoy your day.")
Run:
Password: Process Process-2:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 314, in _bootstrap
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\vdtro\AppData\Roaming\JetBrains\PyCharmCE2023.2\scratches\Password.py", line 25, in loop_main
password = int(input("Password: "))
^^^^^^^^^^^^^^^^^^^
EOFError: EOF when reading a line
This is what it looked like before I used multiprocessing so its results are closer to what they're supposed to be. (Strike can't go down in this version):
import time
Password = 20051402 # password of your choice
strike = 0 # defines strike
while True:
if strike >= 3: # pauses for 5 seconds while counting down
for time_passed in range(5):
print("Timed-out for " + str(5 - time_passed) + " seconds.")
time.sleep(1)
password = int(input("Password: "))
if password == Password:
break # ends loop when correct password is entered
else:
strike += 1 # gives you a strike everytime you get the password wrong
if strike == 1:
print("Two attempts remaining before time-out.")
if strike == 2:
print("One attempt remaining before time-out.")
print("Password correct. Enjoy your day.")
import sysand at beginning of functionloop_mainwritesys.stdin = open(0). For details see stackoverflow.com/questions/30134297/…inputfrom a child process. You haveProcess(target=loop_redemption).terminate(). This will create new process that has not been started but which you are trying to terminate. (1) This is not the process you want to be attempting to terminate and (2) You cannot callterminateon an un-started process. (3)global strikeis not sharable across processes.