Jancer Lima
Jancer Lima

Reputation: 944

How to use exit() in Python 3

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.

Upvotes: 0

Views: 223

Answers (1)

Erik Šťastný
Erik Šťastný

Reputation: 1486

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)

Upvotes: 1

Related Questions