Python 随机模块不工作。ValueError: randrange() 的空范围 (1,1, 0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4513818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random module not working. ValueError: empty range for randrange() (1,1, 0)
提问by user551717
In Python 2.7.1, I import the random module. when I call randint() however, I get the error:
在 Python 2.7.1 中,我导入了 random 模块。但是,当我调用 randint() 时,出现错误:
ValueError: empty range for randrange() (1,1, 0)
This error is caused by an error in the random.py module itself. I don't know how to fix it, not does reinstalling python help. I can't change versions.
此错误是由 random.py 模块本身的错误引起的。我不知道如何修复它,重新安装 python 没有帮助。我无法更改版本。
can someone please give me code for a working module or tell me what to do?
有人可以给我一个工作模块的代码或告诉我该怎么做吗?
采纳答案by Lennart Regebro
You called randint like this:
你这样称呼 randint :
randint(1,0)
That tells randint to return a value starting as 1 and ending at 0. The range of numbers from 1 to zero is as you surely realize an empty range. Hence the error:
这告诉 randint 返回一个从 1 开始到 0 结束的值。从 1 到 0 的数字范围肯定是一个空范围。因此错误:
empty range for randrange()
回答by Katriel
Trust me, randomworks just fine. You are calling randintwith b< a:
相信我,random工作得很好。您正在randint使用b<调用a:
>>> random.randint(1, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\random.py", line 228, in randint
return self.randrange(a, b+1)
File "C:\Python27\lib\random.py", line 204, in randrange
raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop
, width)
ValueError: empty range for randrange() (1,1, 0)
randintreturns a value betweenthe first argument and the second argument.
randint返回第一个参数和第二个参数之间的值。
回答by Rafe Kettler
If you called randint()by itself, it will most definitely result in an error. You need to provide randint()with a range to choose from. randint(a, b), where a and b are integers, should work, and if it doesn't, your Python installation is broken.
如果你自己调用randint(),它肯定会导致错误。您需要提供randint()一个可供选择的范围。randint(a, b),其中 a 和 b 是整数,应该可以工作,如果不可以,则您的 Python 安装已损坏。
It would also raise an exception if b is less than a. Think of it like you're supplying a range: it would make sense to put the lower bound first, right? So put the smaller bound first.
如果 b 小于 a,它也会引发异常。把它想象成你正在提供一个范围:把下限放在第一位是有意义的,对吧?所以把较小的界限放在第一位。
If you really want to compare your randommodule with the correct one, the source is at http://svn.python.org/view/python/branches/release27-maint/Lib/random.py?view=markup
如果您真的想将您的random模块与正确的模块进行比较,来源位于http://svn.python.org/view/python/branches/release27-maint/Lib/random.py?view=markup
回答by MilkyWay90
random.randint(1, 0)returns an error because whenever you use random.randint(a, b)a must be less than b. Try changing random.randint(1, 0)to random.randint(0, 1)to get a valid result.
random.randint(1, 0)返回错误,因为无论何时使用random.randint(a, b)a 都必须小于 b。尝试更改random.randint(1, 0)为random.randint(0, 1)以获得有效结果。
Click herefor more information about random.randint
单击此处了解更多信息random.randint

