如果语句在错误条件下执行

时间:2017-07-05 00:33:19

标签: python python-3.x

if if / elif语句我遇到了这个恼人的问题。我是个新手,抱歉愚蠢的问题。我试图找到一个修复程序,但没有人为Python。 所以,如果两个条件都为True,我希望程序在if子句中执行代码。据我所知,该条款中的代码只有在两个条件都为True时才会执行,我是对的吗?但是,在我的代码中似乎并没有发生这种情况。

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之间的数字。然后,计算机使用random.randint()随机选择0到5之间的数字。 之后,变量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,但也没有。

1 个答案:

答案 0 :(得分:2)

>>> 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

更重要的是,您只需要一个这些条件来执行。不是所有的人。您需要将第二个if及之后的elif替换为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']: ... ,则跳过所有其他条件。

您需要进行一些小改动:

{{1}}