【问题标题】:How do I randomly select an item from a list in Jython using a while loop?如何使用 while 循环从 Jython 的列表中随机选择一个项目?
【发布时间】:2018-03-20 03:36:44
【问题描述】:

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

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

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

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读How to Ask,并考虑添加CVE

标签: list random while-loop jython


【解决方案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)))

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 1970-01-01
    • 2012-09-11
    • 2021-07-21
    • 1970-01-01
    • 2022-01-15
    • 2020-05-21
    • 1970-01-01
    相关资源
    最近更新 更多