[英]Question about the magic 8 ball program with a list from Automate the Boring Stuff with Python
I am having trouble understanding the magic 8 ball program in the Automate the Boring Stuff with Python book.我无法理解《用 Python 自动化无聊的东西》一书中的神奇 8 球程序。 The program basically has a bunch of messages in a list and than uses the random.randint method to choose one of the messages randomly.
该程序基本上在列表中有一堆消息,然后使用 random.randint 方法随机选择其中一条消息。 The code is as follows:
代码如下:
import random
messages = ['It is certain',
'It is decidedly so',
'Yes definitely',
'Reply hazy try again',
'Ask again later',
'Concentrate and ask again',
'My reply is no',
'Outlook not so good',
'Very doubtful']
print(messages[random.randint(0, len(messages) - 1)])
As you can see this program is fairly simple.如您所见,这个程序相当简单。 My question is about the last line.
我的问题是关于最后一行。 Why is he subtracting 1 from the length of messages?
他为什么要从消息长度中减去 1? Shouldn't the random.randint method choose something from all the messages, not just some.
random.randint 方法不应该从所有消息中选择一些东西,而不仅仅是一些。 There are 9 messages, but subtracting one means that only 8 messages are going through to the random.randint method.
有 9 条消息,但减去一条意味着只有 8 条消息通过 random.randint 方法。 Can someone please explain why he is subtracting 1 from the length of messages?
有人可以解释为什么他要从消息长度中减去 1 吗?
Thanks in advance for your reply!提前感谢您的回复!
Python索引从0而不是1开始。如果列表中有9个条目,则需要0-8范围内的随机整数。
messages
is a list. messages
是一个列表。 Lists are indexed from 0 to n-1, where n is the number of elements in the list, which is also the length as returned by the length
function. 列表的索引从0到n-1,其中n是列表中元素的数量,这也是
length
函数返回的length
。 However you don't really need to worry about that, as you can just use 但是,您实际上不必担心,因为您可以使用
print(random.choice(messages))
which will produce the same result as random.choice
will select a random element from a list. 它将产生与random相同的结果
random.choice
将从列表中选择一个随机元素。
Let's say you have a list with 3 objects and you want to print the 3rd element of the list. 假设您有一个包含3个对象的列表,并且要打印列表的第3个元素。
>>> s = [1,2,3]
>>> print(s[3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
This gives the index out of range error because the elements in a list are counted from zero, not one. 由于列表中的元素从零开始计数,而不是从一个开始计数,因此出现索引超出范围错误。
But if you check the length of the list, it will say that this list has 3 elements. 但是,如果您检查列表的长度,它将表示此列表包含3个元素。
>>> s = [1,2,3]
>>> print(len(s))
3
When you pass the len value of the list in random.randint(0,len(s))
, the range becomes 4 (0-3) rather than 3 (0-2). 当您在
random.randint(0,len(s))
传递列表的len值时,范围变为4(0-3)而不是3(0-2)。
To tackle this, we substract the len by one random.randint(0,len(s)-1)
and make the range 3 (0-2). 为了解决这个问题,我们用一个
random.randint(0,len(s)-1)
减去len并将范围设为3(0-2)。
Now randint cannot pick number 3 and pass it to list index, so no more errors. 现在,randint无法选择数字3并将其传递给列表索引,因此不再有错误。
"Shouldn't the random.randint method choose something from all the messages, not just some." “ random.randint方法不应该从所有消息中选择某些东西,而不仅仅是一些。”
The random.randint method does not know anything about the list. random.randint方法对列表一无所知。 If we look at the documentation for randint :
如果我们看一下randint的文档 :
random.randint(a, b)
random.randint(a,b)
Return a random integer N such that a <= N <= b.
返回一个随机整数N,使得a <= N <= b。
So if we do random.randint(1, 3) it will either give us 1, 2, or 3. 因此,如果我们执行random.randint(1,3),它将得到1,2,3。
To get a random number from 0 to 9, we would do as you said, just doing random.randint from 0 to len(messages), and then use that number to get a random index from the list. 要获得从0到9的随机数,我们将按照您所说的做,只需将random.randint从0到len(messages),然后使用该数字从列表中获得随机索引。 The problem is that your list is 9 items, so the indexes are from 0 to 8 (the last item would be messages[8]).
问题在于您的列表有9个项目,因此索引从0到8(最后一个项目是messages [8])。 This is called 0 based indexing.
这称为基于0的索引。 The location of the last index is always equal to the length minus one.
最后一个索引的位置始终等于长度减一。 So that is why we are doing minus one.
所以这就是为什么我们做减一的原因。 I just wanted to clarify and expand this, since some of what you were describing was not quite accurate.
我只想澄清和扩展此内容,因为您所描述的内容不太准确。
print(messages[random.randint(0, len(messages) - 1)])打印(消息[random.randint(0,len(消息)- 1)])
0 is the first index of the list and -1 is the last index of the list. 0 是列表的第一个索引,-1 是列表的最后一个索引。 This line of code prints a random index from the list between the first (0 - in this case "It is certain") and the last (-1 - in this case "Very doubtful). Im pretty amateur but i'm sure I read this in the book yesterday.
这行代码打印列表中第一个(0 - 在本例中为“确定”)和最后一个(-1 - 在本例中为“非常怀疑”)之间的随机索引。我很业余,但我确定我昨天在书上看到这个。
The book states "The benefit of this approach is that you can easily add and remove strings to the messages list without changing other lines of code."该书指出“这种方法的好处是您可以轻松地在消息列表中添加和删除字符串,而无需更改其他代码行。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.