好的,我现在有Python代码,它可以这样做:
if plug in range(1, 5):
print "The number spider has disappeared down the plughole"
但我真正想做的是检查数字是否在范围内而不是。我搜索了一下Python文档,但是什么也找不到。有什么想法吗?
附加数据:运行此代码时:
if not plug in range(1, 5):
print "The number spider has disappeared down the plughole"
我得到以下错误:
Traceback (most recent call last):
File "python", line 33, in <module>
IndexError: list assignment index out of range
我也试过:
if plug not in range(1,5):
print "The number spider has disappeared down the plughole"
返回同样的错误。
使用:
当变量插头超出1到5的范围时,它将打印给定的行
如果您的范围有一个
step
值,那么使用它的性能要快得多:而不是使用其他人建议的
not
方法:证明:
这甚至没有考虑到创建
range
所花费的时间。这似乎也管用:
原问题的确切答案是
if not 1 <= plug < 5:
我想相关问题 更多 >
编程相关推荐