Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打破内部循环_Python_Loops_Security - Fatal编程技术网

Python 打破内部循环

Python 打破内部循环,python,loops,security,Python,Loops,Security,我正在为一门计算机安全课程写一段代码 我正在尝试编写一段测试代码,根据解析时间测试4位密码 我想从4个数字都是0开始。对于每个数字,测试数字0到9 如果I从大于0.2秒的函数返回响应,则用该数字更改pw列表,然后转到下一个 但是,我的代码并没有在列表中递增。 我试着在if语句后简单地插入一个中断,但意识到我还必须中断外部“for循环”,我试着在这里插入一个中断,但仍然无法让代码正常工作 我错过了什么 import time import sys # ignore sys.path.insert

我正在为一门计算机安全课程写一段代码

我正在尝试编写一段测试代码,根据解析时间测试4位密码

我想从4个数字都是0开始。对于每个数字,测试数字0到9

如果I从大于0.2秒的函数返回响应,则用该数字更改pw列表,然后转到下一个

但是,我的代码并没有在列表中递增。 我试着在if语句后简单地插入一个中断,但意识到我还必须中断外部“for循环”,我试着在这里插入一个中断,但仍然无法让代码正常工作

我错过了什么

import time
import sys # ignore

sys.path.insert(0,'.') # ignore
real_password = '2234'

#This function cannot be changed
def check_password(password): # Don't change it
    if len(password) != len(real_password):
        return False
    for x, y in zip(password, real_password):
        time.sleep(0.1) # Simulates the wait time of the safe's mechanism
        if int(x) != int(y):
            return False
    return True



def crack_password():
    pw = [0,0,0,0]
    cnt = 0
    if cnt < 3:
        for i in range (0,9):
            pw[cnt] = i
            start = time.time()
            check_password(create_string(pw))
            stop = time.time()
            t_time = stop - start
            print(pw[cnt], t_time)
            #Struggling with my code here!


#Creates a string from the list
def create_string(pw_list):
    string = ''
    for char in pw_list:
        string += str(char)
    return string

因此,我知道密码的第一个数字是5,因为它大于0.2。然后,我如何从这个循环中中断并转到pw整数的下一个迭代?

这是否回答了您的问题?我之前读过这篇文章,但它并没有真正帮助我,我想我正在努力实现。所以我意识到破坏我代码的不是循环,而是我在if语句上的计时。因此,打破循环的链接是正确的。这不起作用,因为我首先需要检查x的每个迭代,只有当x的迭代大于0.2时,我才需要移动到pw列表中的下一个整数。
def test_pw():
    pw = [0,0,0,0]
    for i in range(4):
        for x in range(0,10):
            pw[i] = x
            pw_test = create_string(pw)
     start_t = time.time()
     check_password(pw_test)
     end_t = time.time()
     t_results = end_t - start_t
     if t_results > 0.2:
def test_pw():
    pw = [0,0,0,0]
    for i in range(4):
        for x in range(0,10):
            pw[i] = x
            pw_test = create_string(pw)
     start_t = time.time()
     check_password(pw_test)
     end_t = time.time()
     t_results = end_t - start_t
     if t_results > 0.2: