Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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 使用if语句终止While循环并中断_Python_Python 3.x_Encryption_Character Encoding_Python 3.6 - Fatal编程技术网

Python 使用if语句终止While循环并中断

Python 使用if语句终止While循环并中断,python,python-3.x,encryption,character-encoding,python-3.6,Python,Python 3.x,Encryption,Character Encoding,Python 3.6,好的,我现在为我创建的加密添加了一点复杂度。 我正在实现一个While循环,从我创建的字典中生成大量字符。然后在将字符添加到变量时进行检查,然后在达到所需字符数后终止while循环。由于某些原因,while循环永远不会终止,即使我使用return0或break等 Alphabet = ["a", "b", "c", "d", "e", "f" "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u

好的,我现在为我创建的加密添加了一点复杂度。 我正在实现一个While循环,从我创建的字典中生成大量字符。然后在将字符添加到变量时进行检查,然后在达到所需字符数后终止while循环。由于某些原因,while循环永远不会终止,即使我使用return0或break等

Alphabet = ["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", "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", '/', '.', ',', '\'', ';', '\\', ']', '[', '{', '}', '|', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '-', '0', '0', '2', '3', '4', '5', '6', '7', '8', '9']

while True:
    generaterandom = random.SystemRandom.choice() 
    calc0 = len(generaterandom)
    print(generaterandom)
    calc02 = 10
    if generaterandom == "10":
        break
这是您的问题,您正在检查
generateradom
是否是一个值为
10
的字符串,您要做的是在它达到10个字符时结束它,因此您需要将if语句更改为:

if len(generaterandom) == 10:
    break
这将更改它以检查变量
generateradom
中的字符数是否等于10,当它等于10时,循环将中断

如果这不起作用,请重新构造循环,使其作用于变量,例如:

loop = True
while loop:
要启动和结束它,请执行以下操作:

if len(generaterandom) == 10:
    loop = False
尝试按如下方式重新构造While循环:

Alphabet = ["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", "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", '/', '.', ',', '\'', ';', '\\', ']', '[', '{', '}', '|', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '-', '0', '0', '2', '3', '4', '5', '6', '7', '8', '9']
bool = 1
while bool == 1:
    generaterandom = random.SystemRandom.choice() 
    calc0 = len(generaterandom)
    print(generaterandom)
    calc02 = 10
    if len(generaterandom) == "10":
        bool = 0

在不进入代码(不运行)的情况下,您的检查可能应该是:if calc0==calc02:
Alphabet = ["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", "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", '/', '.', ',', '\'', ';', '\\', ']', '[', '{', '}', '|', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '-', '0', '0', '2', '3', '4', '5', '6', '7', '8', '9']
bool = 1
while bool == 1:
    generaterandom = random.SystemRandom.choice() 
    calc0 = len(generaterandom)
    print(generaterandom)
    calc02 = 10
    if len(generaterandom) == "10":
        bool = 0