【发布时间】:2012-07-22 16:40:15
【问题描述】:
此代码将成为检查数字是否为质数的程序的一部分。我知道它不是特别优雅,但我想让它只是为了体验而工作。我认为函数失败是因为 if/elif 上的逻辑错误,当我运行这段代码时,它似乎直接进入 else 子句。这是语法问题,还是我不允许在 if 子句中进行逻辑检查?
list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
def find_prime(list, n):
if n in list == False:
list.append(n)
print "I'ts in there now."
elif n in list == True:
print "It's in there already."
else:
print "Error"
find_prime(list, 3)
find_prime(list, 51)
【问题讨论】:
-
你不应该命名一个变量
listPython已经在使用那个标识符了。 -
我不会对所有答案发表评论,我只想在这里说声谢谢,因为它们都有效:)
-
至少接受一个答案....
-
这解释了您当前的问题:stackoverflow.com/questions/9284350/…
标签: python list python-2.7