我如何使用 while 循环从 Jython 的列表中随机 select 一个项目?

How do I randomly select an item from a list in Jython using a while loop?

我需要做一些从列表中随机选择的东西,但要注意的是它必须有一个 while 循环。老实说,我不确定从哪里开始。我在想一个柜台的东西?也许我完全错了。我不擅长这个。

x = ["Bob", "Jim", "Billy", "OtherRandomName"]
counter = 0
while counter < x:
x = x + randrange(1, 5)
x = x + 1

我也在想类似的事情,但我可能离题太远了。任何帮助,将不胜感激。谢谢!

如果您只需要在 while 循环中显示一些随机选择,那么您可以使用这样的代码:

import random

x = ["Bob", "Jim", "Billy", "OtherRandomName"]
max_counter = 10
counter = 0
while counter < max_counter:
    counter += 1
    print('%d %s' % (counter, random.choice(x)))