Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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_Python 3.x - Fatal编程技术网

Python 我的简单假设有什么问题吗。。。埃利夫。。。陈述

Python 我的简单假设有什么问题吗。。。埃利夫。。。陈述,python,python-3.x,Python,Python 3.x,此脚本接受输入的内容并将其格式化为“1/1”类型格式 它与前两个elif语句一起工作,但是当移到第三个语句时,它仍然分配数字前面的2/。它应该跳转到3/和4/,如下所示 非常感谢你的帮助 import re port = input("Enter port number:") if bool(re.search('\/', port)) == True: p = port elif int(port) <= 48: port = "1/" + str(port) e

此脚本接受输入的内容并将其格式化为
“1/1”
类型格式

它与前两个
elif
语句一起工作,但是当移到第三个语句时,它仍然分配数字前面的
2/
。它应该跳转到
3/
4/
,如下所示

非常感谢你的帮助

import re

port = input("Enter port number:")

if bool(re.search('\/', port)) == True:
    p = port

elif int(port) <= 48:
    port = "1/" + str(port)

elif int(port) >= 53 <= 100:
    port = "2/" + str(port)

elif int(port) >= 105 <= 152:
    port = "3/" + str(port)

elif int(port) >= 157 <= 204:
    port = "4/" + str(port)

print(port)
重新导入
端口=输入(“输入端口号:”)
如果布尔(重新搜索('\/',端口))==True:
p=端口

elif int(port)=53=105=157问题在于您试图链接比较:

elif int(port) >= 53 <= 100:

这仅适用于
int(端口)
介于53和100(含)之间的情况。您需要对其余的
elif
块进行类似的更改。

您的if条件混淆了。修正:

def getPort(port):

    # https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain
    # only convert to int once, if that does not work check for / else bail out
    try:
        p = int(port)
    except ValueError:
        if "/" in port: # no need for regex
            return port
        else:
            raise ValueError("Port either integer or somthing with / in it")

    if p <= 48:               # 49-52 are not covered
        port = "1/" + port

    elif  53 <= p <= 100:     # move the condition between, the way you did it theyre True
        port = "2/" + port    # no matter because 53 <= 100 all the time

    elif 105 <= p <= 152:     # 101-104 are not covered
        port = "3/" + port

    elif 157 <= p <= 204:     # 152-156 are not covered
        port = "4/" + str(port)

    else:
        raise ValueError("Port either integer or somthing with / in it")

    return port

for p in ["1","54","99","121","180","47/11","2000"]:
    try:
        print(getPort(p))
    except ValueError as e:
        print (e)

您缺少一些端口范围,50 f.e.不包括在内,这将导致ValueError。

您的错误如下:

elif int(port) >= 53 <= 100:

port=“1/”+str(端口)
。。。端口已经是str了…在哪里使用变量p?我看到您分配了它(当re.search生成一个True时),但从未使用它。此脚本只是稍后将使用的脚本的一小部分。这就是为什么你会看到p变量。我无法继续前进,直到我先弄清楚这些步骤。这就成功了。太棒了@帕特里卡特纳,我不这么认为。我在回答中添加了一个到python文档的链接,该链接部分的第三段使用了
。对不起,我错了。
# Input: ["1","54","99","121","180","47/11","2000"]

1/1
2/54
2/99
3/121
4/180
47/11
Port either integer or somthing with / in it
elif int(port) >= 53 <= 100:
elif int(port) >= 53 and 53 <= 100:
port = input("Enter port number:")
int_port = int(port)    # so we don't repeat the same operation multiple unnecessary times

if bool(re.search('\/', port)):
    pass
elif int_port <= 48:
    port = "1/" + port
elif 53 <= int_port <= 100:
    port = "2/" + port
elif 105 <= int_port <= 152:
    port = "3/" + port
elif 157 <= int_port <= 204:
    port = "4/" + port

print(port)