Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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_If Statement - Fatal编程技术网

Python 如果条件表达式不工作

Python 如果条件表达式不工作,python,if-statement,Python,If Statement,我有两个具有以下值的列表 List1=['6', '9', '16', '19', '0', '3', '6', '0', '6', '12', '18'] List2=['9', '16', '19', '24', '3', '6', '19', '6', '12', '18', '24'] 下面是我的python代码中的循环,其中if条件不存在 idk为60时工作,时间=60/60=1k 在这种情况下,当列表1K具有“0”且列表2具有“3”时,它应该进入if条件。但是,if条件不起作用。

我有两个具有以下值的列表

List1=['6', '9', '16', '19', '0', '3', '6', '0', '6', '12', '18']

List2=['9', '16', '19', '24', '3', '6', '19', '6', '12', '18', '24']
下面是我的python代码中的循环,其中if条件不存在 idk为60时工作,时间=60/60=1k

在这种情况下,当列表1K具有“0”且列表2具有“3”时,它应该进入if条件。但是,
if
条件不起作用。我还尝试使用以下表达式:

if ((time >=List1[i]) and (time <=List2[i])):

if((time>=List1[i])和(time List1[i])或(time==List1[i])和((time您正在比较字符串和整数。数字总是在字符串之前排序,因此
time
总是低于
List1[i]
List2[i]

请在列表中使用整数:

List1 = [6, 9, 16, 19, 0, 3, 6, 0, 6, 12, 18]
List2 = [9, 16, 19, 24, 3, 6, 19, 6, 12, 18, 24]
Python2尝试使所有内容都有序,这就是为什么比较字符串和整数是合法的,但也意味着您可能会犯类似的错误。Python3取消了这一目标,而尝试将字符串与整数进行比较会产生错误

请注意,您也在使用整数除法,因为
/
除法运算符的两个操作数都是整数。您可能希望改用浮点除法:

time = id / 60.0
虽然我不能完全确定您对除法的预期输出。使用整数除法,
60/60
61/60
62/60
的结果总是
1

您的
if
表达式可以通过比较链接简化:

if List1[i] <= time <= List2[i]:
这将产生:

>>> List1 = [6, 9, 16, 19, 0, 3, 6, 0, 6, 12, 18]
>>> List2 = [9, 16, 19, 24, 3, 6, 19, 6, 12, 18, 24]
>>> for id in range(60, 63):
...     time = id / 60.0
...     for lower, higher in zip(List1, List2):
...         if lower <= time <= higher:
...             print 'inside if with lower={}, higher={}, time={}'.format(lower, higher, time)
... 
inside if with lower=0, higher=3, time=1.0
inside if with lower=0, higher=6, time=1.0
inside if with lower=0, higher=3, time=1.01666666667
inside if with lower=0, higher=6, time=1.01666666667
inside if with lower=0, higher=3, time=1.03333333333
inside if with lower=0, higher=6, time=1.03333333333
列表1=[6,9,16,19,0,3,6,0,6,12,18] >>>清单2=[9,16,19,24,3,6,19,6,12,18,24] >>>对于范围(60,63)内的id: …时间=id/60.0 …对于较低、较高的拉链(列表1、列表2): …如果低于请尝试:

python2
>>>int
这是因为要比较整数和字符串,请参见:

除数字外,不同类型的对象按其类型排序 名称;不支持正确比较的相同类型的对象 按地址排序 这在Python3中是“固定的”:

python3
>>> int < str
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: type() < type()
这在Python3中是“固定的”:

python3
>>> int < str
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: type() < type()
python3
>>>int
问题是因为您正在比较浮点值和字符串值。时间转换为浮点值,而List1和List2具有字符串值。若要克服此错误,请将List1和List2声明为整数:

List1=[6, 9, 16, 19, 0, 3, 6, 0, 6, 12, 18]                                       
List2=[9, 16, 19, 24, 3, 6, 19, 6, 12, 18, 24]
另外,我在第二个for循环后面的if语句中看到缩进错误。代码应该是:

for id in range(60,63):
    time = id/ 60
    for i in range(0, len(List1) - 1):
        if ((time >=List1[i]) and (time <=List2[i])):
            print("inside IF")

对于zip中的L1、L2(列表1、列表2),Python3也修复了一个设计错误,这可能值得做
@shuttle87:OP声明他们也尝试过。
id/60
为您范围内的所有值返回
1
。@elyon:很高兴您发现多个答案很有用。请考虑到您可以将其中一个标记为已接受,通常您会选择对您帮助最大的答案。选择权完全属于您,但只保留一个。:-)请参见OP未比较浮点值,除法将生成整数。这是Python 2,而不是3(请参见
print
语句),如果是Python 3,代码将引发异常(
TypeError:unorderable types:float()>str()
)是的,你是对的。我在回答中说我使用的是Python 3.3和Python 2。区别很重要,并且与问题绝对相关(例如,你没有回答OP为什么首先看到问题,这是不正确的)。OP试图将所有答案标记为“有帮助”,最后是您的答案,因此现在我们的情况似乎是您的答案最有帮助。但充其量也是不完整的。
List1=[6, 9, 16, 19, 0, 3, 6, 0, 6, 12, 18]                                       
List2=[9, 16, 19, 24, 3, 6, 19, 6, 12, 18, 24]
for id in range(60,63):
    time = id/ 60
    for i in range(0, len(List1) - 1):
        if ((time >=List1[i]) and (time <=List2[i])):
            print("inside IF")
time=int(id/60)