【问题标题】:If statements executed even with false condition即使在错误条件下也执行了 if 语句
【发布时间】:2023-03-19 05:15:01
【问题描述】:

我在使用 if/elif 语句时遇到了这个烦人的问题。我是新手,不好意思问了个愚蠢的问题。我试图找到一个修复程序,但没有人为 Python 提供它。 所以,如果两个条件都为真,我希望程序执行if 子句中的代码。据我所知,子句中的代码只有在两个条件都为真时才会执行,对吗?不过,这似乎并没有在我的代码中发生。

result = userNumber + randomNumber
if not result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is odd, so you lost :(')
if result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is even, so you won :)')
if not result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is odd, so you won :)')
if result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is even, so you lost :(')

所以,userNumberrandomNumber 变量是之前设置的。在这个游戏中,发生的情况是:用户选择偶数或奇数,并输入一个从 0 到 5 的数字。然后,计算机随机选择一个从 0 到 5 的数字,使用random.randint()。 之后,变量result 设置为userNumber + randomNumber 的总和。如果该和的结果是奇数并且用户选择了奇数,则用户获胜,如果用户选择了偶数,则用户输了。如果总和是偶数,则正好相反:如果前一个总和结果的结果是偶数并且用户选择了偶数,则用户获胜,如果用户选择了奇数,则用户输了。 希望你能理解!

所以,我的代码现在的问题是它出于某种原因执行了所有四个 IF 语句,所以最终输出如下所示:

Welcome to the Even or Odd game!

Type letter 'o' if you want odd and the letter 'e' if you want even.

Your choice:o

Now, type in a number from 0 to 5:2

Your number: 2

Computer's number: 5

Adding these two numbers together, we get 7

That number is odd, so you lost :(

That number is even, so you won :)

That number is odd, so you won :)

That number is even, so you lost :(

代码如下:

import random
import time

print ('Welcome to the Even or Odd game!')
print ('Type letter \'o\' if you want odd and the letter \'e\' if you want even.')

userChoice = input('Your choice: ').lower()
time.sleep(1)
userNumber = int(input('Now, type in a number from 0 to 5: '))

randomNumber = random.randint(0,5)
time.sleep(2)

print ('Your number: ' + str(int(userNumber)))
time.sleep(2)
print ('Computer\'s number: ' + str(int(randomNumber)))
time.sleep(2)

result = userNumber + randomNumber
print (str(result))
print ('Adding these two numbers together, we get ' + str(result))
if not result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is odd, so you lost :(')
if result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is even, so you won :)')
if not result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is odd, so you won :)')
if result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is even, so you lost :(')

有什么想法吗?抱歉发帖太长,如有重复请见谅!我只是没有在互联网上找到任何答案:/ 非常感谢!

编辑:我也尝试使用 elif 语句而不是所有 if,但也没有用。

【问题讨论】:

标签: python python-3.x


【解决方案1】:
>>> if False or 'even':
...     print('This shouldn\'t work')
... 
This shouldn't work

非空字符串的真值是True。尽管您的其他条件是False,但由于您编写条件的方式,您仍在执行这些语句。

就目前而言,这是评估您的条件的方式:

if (not result % 2 == 0) and ((userChoice == 'e') or ('even')):
# __________1________________________2_________________3_____

(1) 是True。 (2) 是False。但是(3)是True(因为字符串的真实性)。所以你执行第一个if

此外,您只希望执行这些条件中的一个。不是所有的人。您需要用elif 替换您的第二个if 及以后。因此,如果一个条件是True,则跳过所有其他条件。

您需要做一些小改动:

if not result % 2 == 0 and userChoice in ['e', 'even']:
    ...
elif result % 2 == 0 and userChoice in ['e', 'even']:
    ...
elif not result % 2 == 0 and userChoice in ['o', 'odd']:
    ...
elif result % 2 == 0 and userChoice ['o', 'odd']:
    ...

【讨论】:

  • 非常感谢!
  • @T.Licht 如果这个答案有帮助,请考虑接受它。 :) 谢谢。
  • @T.Licht 最好只是提出一个新问题。提供完整的追溯。 :) 在 cmets 中很难调试。
  • 好的!谢谢:)
  • @T.Licht 它对我有用。我运行了你的代码。没有问题。请考虑提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多