0

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.")
4
  • You can try to import sys and at beginning of function loop_main write sys.stdin = open(0). For details see stackoverflow.com/questions/30134297/… Commented Sep 30, 2023 at 14:42
  • you could skip putting the main loop in a process. let it run out of the original thread. The error is because you have not supplied stdin for the process. you will have issues with the global variable too because you are using processes. review: docs.python.org/3/library/… Commented Sep 30, 2023 at 14:49
  • Threading is better way to go if you need a shared object (strike) (threads are in process vs having separate processes (shared memory vs separate memory) docs.python.org/3/library/threading.html Commented Sep 30, 2023 at 14:51
  • 1
    You cannot call input from a child process. You have Process(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 call terminate on an un-started process. (3) global strike is not sharable across processes. Commented Oct 3, 2023 at 11:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.