0
import sys
import time
start = time.time()

key = input('Type your key:')
x = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
     '1', '2', '3', '4', '5', '6', '7', '8', '9')

for x1 in x:
    for x2 in x:
        for x3 in x:
            y = (x1+x2+x3)
            print (y) 
            if y == key:
                print('Founded')
                exit()
done = time.time()
elapsed = done - start
print(elapsed)

The code don't stop using the exit(), the program must finish all possibilities to stop.

Jancer Lima
  • 744
  • 2
  • 10
  • 19
  • 1
    I do not understand your question. Note, you shouldn't use the `exit` function outside of the repl – juanpa.arrivillaga Dec 06 '19 at 00:24
  • 1
    This code is not indented correctly. Please also explain why you believe `exit()` isn't stopping the program; what is the expected output vs. actual output? – kaya3 Dec 06 '19 at 00:24
  • The code is trying to figure out the key, it figures out but keep trying until all possibilities are tried. The exit() is to stop when the key is found – Jancer Lima Dec 06 '19 at 00:28
  • 1
    Does this answer your question? [Python exit commands - why so many and when should each be used?](https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used) – divibisan Dec 06 '19 at 00:31
  • 1
    Looks like you should have a function there that **returns** the value. – Klaus D. Dec 06 '19 at 00:33
  • try `sys.exit()` – Maxxik CZ Dec 06 '19 at 08:27

1 Answers1

1

You should put it inside of function and use return with "founded" flag.

There is no reason to use of exit() for such a trivial thing.

import sys
import time

def check():
    for x1 in x:
        for x2 in x:
            for x3 in x:
                y = (x1+x2+x3)
                print (y) 
                if y == key:
                    return True
    return False


start = time.time()

key = input('Type your key:')
x = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
     '1', '2', '3', '4', '5', '6', '7', '8', '9')

res = check()
if res:
    print("Found")

done = time.time()
elapsed = done - start
print(elapsed)
Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41