Python脚本永远“执行文件”

时间:2018-01-03 18:41:09

标签: python qr-code

这是我的剧本:

def encodage(message):
    """
    begins our binary qrcode with the type of the message:
    binary, numeric or alphanumeric (string)
    """
    qrcode = []



    if type(message) == str: #string
        qrcode = [0,0,1,0] + qrcode

    else: 
        if type(message) == list: #list can contain numeric and alphanumeric
            if type(message[0]) == int:
                bit = True

                while bit:
                    for n in message:
                        if (n != 0) and (n != 1): #verifying if binary
                            bit = False
                if bit:
                    qrcode = [0,1,0,0] + qrcode
                else:
                    qrcode = [0,0,0,1] + qrcode

            if type(message[0]) == str:
                qrcode = [0,0,1,0] + qrcode
    return qrcode

我认为它做得很清楚。当我使用以下内容运行它时:

message = [0,1,1,1,1]

print(encodage(message))

没有给出答案,它只是永远转变。

我认为问题来自我奇怪的二进制测试循环(适用于message = "salut")。

你怎么看?谢谢你的阅读。

3 个答案:

答案 0 :(得分:0)

你看不出自己的状况吗?

message = [0,1,1,1,1]

bit = True
while bit:
  for n in message:
    if (n != 0) and (n != 1): #verifying if binary
     bit = False

这个

if (n != 0) and (n != 1): #verifying if binary

将永远返回False因此永远循环,只需将您的输入(消息)与此if语句进行比较,看看它是否有意义

答案 1 :(得分:0)

感谢几位评论,这里有一个更正:

    if type(message) == list:
        if type(message[0]) == int:
            bit = True

            for n in message:
                if (n != 0) and (n != 1):
                    bit = False

谢谢大家。

答案 2 :(得分:0)

while循环没有任何好处。只需删除while即可。将空列表添加到结果中也没有意义:

def encodage(message):
    """
    begins our binary qrcode with the type of the message:
    binary, numeric or alphanumeric (string)
    """
    if isinstance(message, str):
        qrcode = [0,0,1,0]
    elif isinstance(message, list):
        if isinstance(message[0], int):
            # verifying if binary
            bit = all(n in (0, 1) for n in message)
            qrcode = [0,1,0,0] if bit else [0,0,0,1]
        elif isinstance(message[0], str):
            qrcode = [0,0,1,0]
        else:
            qrcode = []
    else:
        qrcode = []
return qrcode