【发布时间】:2015-10-26 23:05:02
【问题描述】:
这是我的代码,我无法让最后一个 IF 语句正常工作,我希望它仅在答案不是偶数或奇数时打印,但几乎每次都会打印。我该如何解决?
def evenOdd(x):
for x in range (1, 6):
num= random.randrange(1,101)
ans=raw_input("Is" + str(num) + "Odd or Even?")
if (num % 2 == 0 and ans=="even") or (num % 2 == 1 and ans == "odd"):
print "correct"
if (num % 2 == 0 and ans=="odd") or (num % 2==1 and ans == "even"):
print "incorrect"
if (num % 2 ==0 or num % 2 == 1 and ans != "odd" or ans != "even"):
print "Please answer with Even or Odd"
【问题讨论】:
-
num % 2 ==0 or num % 2 == 1- 为什么会比这更进一步?其中之一肯定是真的!你为什么不直接测试if ans != "odd" and ans != "even":,因为哪个号码并不重要?
标签: python if-statement jes