簡體   English   中英

打破內循環 Python

[英]Breaking out of an inner loop Python

我正在為我正在為計算機安全課程編寫的一段代碼而苦苦掙扎。

我正在嘗試編寫一段測試代碼,根據解析時間測試 4 位密碼。

我想從 4 個全 0 的數字開始。 對於每個數字,測試從 0 到 9 的數字。

如果我從 function 收到大於 0.2 秒的響應,則使用該數字更改 pw 列表,然后移至下一個。

但是我的代碼並沒有在列表中遞增。 我嘗試在我的 if 語句之后簡單地插入一個 break,但意識到我還必須打破我們的外部“for 循環”,我嘗試在這里插入一個 break,但仍然無法讓我的代碼工作。

我錯過了什么?

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

我在crack_password function 中的打印語句提供:

0 0.10016393661499023
1 0.10017132759094238
2 0.1006174087524414
3 0.10022568702697754
4 0.1002652645111084
5 0.20053935050964355
6 0.10025167465209961
7 0.10107183456420898
8 0.10021018981933594

因此我知道密碼的第一個數字是 5,因為它大於 0.2。 然后我怎樣才能打破這個循環並移動到 pw integer 的下一次迭代?

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:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM