Mar-21-2024, 09:33 PM
(This post was last modified: Mar-21-2024, 09:33 PM by deanhystad.)
What is the problem you are trying to solve?
If there are no restriction on PIN's other than they have 6 digits, you are already guessing the pin in the most efficient manner possible. If multi-processing is an option, you could break up the task into multiple ranges.
But I'm guessing there is more information in the problem description than you have shown.
Your program is structured wrong. You should not put the timer calls in the thing you want to time, and a function that guesses a pin should return the guessed pin number instead of printing the pin number. I would write the program like this:
If there are no restriction on PIN's other than they have 6 digits, you are already guessing the pin in the most efficient manner possible. If multi-processing is an option, you could break up the task into multiple ranges.
But I'm guessing there is more information in the problem description than you have shown.
Your program is structured wrong. You should not put the timer calls in the thing you want to time, and a function that guesses a pin should return the guessed pin number instead of printing the pin number. I would write the program like this:
from time import time def find_pin(pin): # Validate input try: if len(pin) != 6: return None pin = int(pin) for guess in range(1000000): if guess == pin: return pin except ValueError: pass return None pin = input("Enter PIN: ") start = time() if find_pin(pin) is None: print("Entered pin is invalid") else: print(f"Found pin in {time() - start} seconds.")