相关疑难解决方法(0)

在Python中,`不是int`是否会返回false?

我正在使用参数解析器允许将端口号传递给我的程序.然后我尝试验证该值,并且第一个测试之一是is int测试:

    parser = argparse.ArgumentParser(description='Provides XML-RPC API.')
    parser.add_argument('--port', dest='port', default=9666, type=int,
                       help='port to listen on, 9666 by default.');
    args = parser.parse_args()

    if args.port is not int:
        raise TypeError("Port must be integer (%s given), use -h argument to get help."%(type(args.port).__name__))
    if args.port>=65536 or args.port<=0:
        raise IndexError("Port must be lower than 65536, 65535 is the maximum port number. Also, port can only be positive, non-zero number. Use -h argument to get help.")
    if args.port<=1204:
        print("Notice: ports up to 1024 are …
Run Code Online (Sandbox Code Playgroud)

python argparse

3
推荐指数
1
解决办法
1226
查看次数

比较python中的两个布尔表达式

In[19]: x = None
In[20]: y = "Something"
In[21]: x is None == y is None
Out[21]: False
In[22]: x is None != y is None ## What's going on here?
Out[22]: False
In[23]: id(x is None)
Out[23]: 505509720
In[24]: id(y is None)
Out[24]: 505509708
Run Code Online (Sandbox Code Playgroud)

为什么Out [22]错了?他们有不同的ID,所以这不是身份问题....

python

3
推荐指数
1
解决办法
93
查看次数

与使用“is”的关键字“list”和“set”相比,为什么空列表和集合上的 type() 会产生不同的答案?

我目前正在学习 Python 并且正在回答一个练习测验的问题,在那里我遇到了以下问题:

  1. print(type({}) is set) 的输出是什么?
  2. print(type([]) is list) 的输出是什么?

我不确定如何处理 Q1 的理由。产生 False,但 Q2。产生真。我正在遵循详细介绍关键字is细微差别的相关帖子中的指导,但是在比较列表与集合时,我看不出两者的不同之处。

python list set python-3.x

1
推荐指数
1
解决办法
25
查看次数

Python无限循环引起或复合条件但不使用not()时

    test = ''

# This loop infinitely

    while test != 'O' or test != 'X':
        test = raw_input("Enter: ").upper()

# This works fine   

    while not(test == 'O' or test == 'X'):
        test = raw_input("Enter: ").upper()
Run Code Online (Sandbox Code Playgroud)

推杆not和使用之间有什么区别!=

python

0
推荐指数
1
解决办法
131
查看次数

ZeroDivisionError:即使我有一个零捕手,浮点除零

我对Python有点新鲜.我在下面附上了一段代码.constant_a&b是整数.运行此代码时,我收到以下错误:

回溯(最近一次调用最后一次):文件"U:\ V10_run2\process.py",第209行,delta_mcs_2_gfx_percentage =(delta_mcs_2_gfx*100)/ float(mcs)ZeroDivisionError:浮点除零

mcs=hash["MCF"]*constant_a/constant_b  

if mcs is 0:
      delta__percentage=-100
else:
      delta__percentage=(delta*100)/mcs
Run Code Online (Sandbox Code Playgroud)

正如错误所说,我认为这是因为python试图进行整数除法float(delta*100)/float(mcs)并将mcs舍入为0但我也尝试过这也没有帮助.有什么建议 ??

python integer-division divide-by-zero floating

0
推荐指数
1
解决办法
949
查看次数

Python 代码与 if 的比较验证 False 当它应该是 True 时

我在下面有以下 Python 代码。我期待代码返回,True但是当我运行它时,它似乎总是返回False. 检查 361 是否为 361 时似乎失败,但我不知道为什么:

def comp(array1, array2):
    if array1 is None or array2 is None or len(array1) is 0 or len(array2) is 0 or len(array1) is not len(array2):
        return False

    aListSquared = [x * x for x in sorted(array1)]
    array2.sort()

    print(aListSquared)
    print(array2)

    for x in range(len(aListSquared)):
        print('{0}:{1}'.format(aListSquared[x], type(aListSquared[x])))
        print('{0}:{1}'.format(array2[x], type(array2[x])))

        if int(aListSquared[x]) is not int(array2[x]):
            return False

    return True


a1 = [121, 144, 19, 161, 19, 144, 19, 11]
a2 = [11 …
Run Code Online (Sandbox Code Playgroud)

python integer comparison-operators

0
推荐指数
1
解决办法
88
查看次数