Python:使用异常进行迭代(初学者)

时间:2022-03-30 18:22:02

I just want to know why this doesn't work (I am trying to name the ducklings from a book: Jack, Kack, Lack, Mack, Nack, Ouack, Pack, Quack) Note: Quack and Ouack have a U

我只是想知道为什么这不起作用(我试图从一本书中给小鸭子起名:Jack,Kack,Lack,Mack,Nack,Ouack,Pack,Quack)注意:Quack和Ouack有一个U

prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
    if letter != 'O' or 'Q':      #I know this doesn't work, need to know alternative
        print letter + suffix
    else:
        print letter + 'u' + suffix

3 个解决方案

#1


7  

You likely mean this:

你可能是这个意思:

if letter != 'O' or letter != 'Q':

The result of your original statement,

原始陈述的结果,

if letter != 'O' or 'Q':

compared letter to the result of 'O' or 'Q', which is a boolean (true to be exact) (so you could see why this comparison would always be true as it was).

将字母与'O'或'Q'的结果进行比较,这是一个布尔值(确切地说是真的)(所以你可以看到为什么这种比较总是如此)。

#2


5  

Please note that

请注意

if letter != 'O' or 'Q':

is in fact

实际上是

if (letter != 'O') or 'Q':

That is probably not what you wanted.

那可能不是你想要的。

Just a small test on top of it:

只是一个小测试:

>>> True != False or True
True
>>> (True != False) or True
True
>>> True != (False or True)
False

Note: This means that the answer marked on top is not true, letter is not compared to the result of O or Q...

注意:这意味着顶部标记的答案不正确,字母不与O或Q的结果进行比较...

#3


2  

Python is not COBOL or some other language which supports this syntax. As a start I would suggest you read Expressions.

Python不是COBOL或其他支持此语法的语言。首先,我建议你阅读表达式。

Now coming back to your problem, what you expect from the statement

现在回到你的问题,你对该声明的期望

if letter != 'O' or 'Q':

definitely

无疑

if letter != 'O' or letter != 'Q':

interestingly Python allows you to think laterally. For example you might also say,

有趣的是,Python允许你横向思考。例如,你也可以说,

letter not in ['O','Q'] 

or simply

或简单地说

letter not in 'OQ': #In Python Notation

or can be more expressive like

或者可以更具表现力

if all(letter != x for x in 'OQ'):

Just compare the above mentioned syntax and usage with yours

只需将上述语法和用法与您的语法和用法进行比较

When you wrote

你写的时候

if letter != 'O' or 'Q':

which in Python should be written as

在Python中应该写成

if letter not in 'OQ':

or may even be

甚至可能是

if all(letter != x for x in 'OQ'):

#1


7  

You likely mean this:

你可能是这个意思:

if letter != 'O' or letter != 'Q':

The result of your original statement,

原始陈述的结果,

if letter != 'O' or 'Q':

compared letter to the result of 'O' or 'Q', which is a boolean (true to be exact) (so you could see why this comparison would always be true as it was).

将字母与'O'或'Q'的结果进行比较,这是一个布尔值(确切地说是真的)(所以你可以看到为什么这种比较总是如此)。

#2


5  

Please note that

请注意

if letter != 'O' or 'Q':

is in fact

实际上是

if (letter != 'O') or 'Q':

That is probably not what you wanted.

那可能不是你想要的。

Just a small test on top of it:

只是一个小测试:

>>> True != False or True
True
>>> (True != False) or True
True
>>> True != (False or True)
False

Note: This means that the answer marked on top is not true, letter is not compared to the result of O or Q...

注意:这意味着顶部标记的答案不正确,字母不与O或Q的结果进行比较...

#3


2  

Python is not COBOL or some other language which supports this syntax. As a start I would suggest you read Expressions.

Python不是COBOL或其他支持此语法的语言。首先,我建议你阅读表达式。

Now coming back to your problem, what you expect from the statement

现在回到你的问题,你对该声明的期望

if letter != 'O' or 'Q':

definitely

无疑

if letter != 'O' or letter != 'Q':

interestingly Python allows you to think laterally. For example you might also say,

有趣的是,Python允许你横向思考。例如,你也可以说,

letter not in ['O','Q'] 

or simply

或简单地说

letter not in 'OQ': #In Python Notation

or can be more expressive like

或者可以更具表现力

if all(letter != x for x in 'OQ'):

Just compare the above mentioned syntax and usage with yours

只需将上述语法和用法与您的语法和用法进行比较

When you wrote

你写的时候

if letter != 'O' or 'Q':

which in Python should be written as

在Python中应该写成

if letter not in 'OQ':

or may even be

甚至可能是

if all(letter != x for x in 'OQ'):