[英]`JES - Python, Even and Odd
我需要编写一个程序,询问随机数是偶数还是奇数,用户输入答案,然后程序会告诉您它是正确还是错误。 我不知道要输入ans=raw_input("is" [random number] "Odd or Even?")
这就是我现在已经实施的方法,该怎么解决?
def evenOdd():
num=random.randrange(1,101)
ans=raw_input("is"+num+"even or odd?")
if ans % 2 == 0:
print "correct"
elif ans % 2 == 1:
print "incorrect"
看看python的random模块。 您可能想要诸如random.randint(1,100)
类的东西。
这应该可以解决您的问题-
def evenOdd():
num = random.randrange(1,101)
ans = raw_input("Is "+str(num)+" even or odd?")
if (num % 2 == 0 and ans == "even") or (num % 2 != 0 and ans == "odd"):
print "Correct answer"
else:
print "Incorrect answer"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.