如何在Python3中使用exit()

2024-04-26 23:59:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

如果代码不停止使用exit(),程序必须完成所有可能的停止操作。你知道吗


Tags: keyinimportforinputtimesysexit
1条回答
网友
1楼 · 发布于 2024-04-26 23:59:07

您应该将它放在函数中,并使用带有“founded”标志的return。你知道吗

对于这样一件小事,没有理由使用exit()。你知道吗

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)

相关问题 更多 >