2017-10-09 56 views
0

我需要做一些随机从列表中随机挑选某些东西的东西,但问题在于它必须带有一个while循环。我真的不知道从哪里开始。我正在想一个柜台的东西?也许我完全错了。我不擅长这一点。如何使用while循环从Jython的列表中随机选择一个项目?

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

我在想那样的事情,但我可能会离开。任何帮助,将不胜感激。谢谢!

+0

欢迎StackOverflow上。请阅读[如何提问](https://stackoverflow.com/help/how-to-ask),并考虑包含[CVE](https://stackoverflow.com/help/mcve)。 – Antimony

回答

0

如果你需要的是显示在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))) 
相关问题