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.

6
  • 1
    I do not understand your question. Note, you shouldn't use the exit function outside of the repl Commented Dec 6, 2019 at 0: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? Commented Dec 6, 2019 at 0: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 Commented Dec 6, 2019 at 0:28
  • 1
    Does this answer your question? Python exit commands - why so many and when should each be used? Commented Dec 6, 2019 at 0:31
  • 1
    Looks like you should have a function there that returns the value. Commented Dec 6, 2019 at 0:33

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

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.