请谁能告诉我为什么输出是这样的? (蟒蛇)

时间:2019-10-04 18:55:26

标签: python-3.x

我是python的初学者,我不知道为什么这段代码的输入不如我期望的那样

my input

我是python的初学者,我不知道为什么这段代码的输入不如我期望的那样

def count_smileys(arr):
    #the number of valid smiley faces in array/list
    smileys_count = 0
    for character in arr:
        if len(character) == 2:
            if character[0] == ":" or character[0] == ";" and character[2] == ")" or character[2] == "D":
                smileys_count += 1
        if len(character) == 3:
            if character[0] == ":" or character[0] == ";" and character[2] == ")" or character[2] == "D" and character[2] == "-" or character[2] == "~":
                smileys_count +=1

    return(smileys_count)

输出为:

my output

2 个答案:

答案 0 :(得分:1)

您错过了括号,当您将句子AND / OR混合在一起时,通常需要括号:

Total: 4 processes with 4 diagrams
MG5_aMC>display diagrams
Drawing Process: u u~ > z > mu+ mu- WEIGHTED<=4 @1
Wrote file /tmp/diagrams_1_uux_z_mupmum.eps
open /tmp/diagrams_1_uux_z_mupmum.eps
Drawing Process: c c~ > z > mu+ mu- WEIGHTED<=4 @1
Wrote file /tmp/diagrams_1_ccx_z_mupmum.eps
open /tmp/diagrams_1_ccx_z_mupmum.eps
Drawing Process: d d~ > z > mu+ mu- WEIGHTED<=4 @1
Unhandled exception in thread started by <function call at 
0x7fbbb476bc08>Wrote file /tmp/diagrams_1_ddx_z_mupmum.eps

Traceback (most recent call last):
open /tmp/diagrams_1_ddx_z_mupmum.eps
File "/usr/lib/python2.7/subprocess.py", line 172, in call
Drawing Process: s s~ > z > mu+ mu- WEIGHTED<=4 @1
Unhandled exception in thread started by <function call at 0x7fbbb476bc08>
Traceback (most recent call last):
File "/usr/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
Wrote file /tmp/diagrams_1_ssx_z_mupmum.eps
open /tmp/diagrams_1_ssx_z_mupmum.eps
raise child_exception
time to draw 0.063835144043
raise child_exception
OSErrorOSError: [Errno 2] No such file or directory: [Errno 2] No such 
file or directory
MG5_aMC>
Unhandled exception in thread started by <function call at 0x7fbbb476bc08>
Traceback (most recent call last):
File "/usr/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Unhandled exception in thread started by <function call at 0x7fbbb476bc08>
Traceback (most recent call last):
File "/usr/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

此外,您的索引有误

答案 1 :(得分:1)

这是逻辑运算符优先级问题:and的优先级高于or,就像*的优先级高于+一样。您应该使用括号。

if len(character) == 2:
  if (character[0] == ":" or character[0] == ";") and (character[1] == ")" or character[1] == "D"):
    smileys_count += 1

另请参阅Priority of the logical statements NOT AND & OR in python