首页 > 解决方案 > while 循环永远不会结束(python)

问题描述

尝试过检查生成的私钥是否长度相等,但程序会卡在这个 while 循环中?

    private_key_hex = '{:x}'.format(private_key_dec)
    print ("Length of private_key_hex: ", len(str(private_key_hex)))  

    while (len(private_key_hex) % 2) != 0:  
       private_key_hex = '{:x}'.format(private_key_dec)

    print("Private key (Hex):        " + private_key_hex)  # this never happens...

标签: pythonwhile-loop

解决方案


即使不知道 的来源private_key_dec,我也可以看到您正在循环一个字符串,并且由于您没有更改 的值private_key_dec,因此您实际上private_key_hex每次都重新分配给相同的值。
因此,如果它进入 while 循环,它永远不会离开。

一种解决方案是将您分配private_key_dec给一个列表,而不是while循环,使用一个for循环到这个列表。但为了更准确,请详细说明您接收数据的格式。


推荐阅读