Search code examples
listrandomwhile-loopjython

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


I need to make something that randomly picks something from a list randomly, but the catch is that it has to be with a while loop. I honestly am not sure where to start. I was thinking something with a counter? Maybe I am completely wrong. I am not good at this.

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

I was thinking something like that, but I am probably way off. Any help would be appreciated. Thanks!


Solution

  • If all you need is to show some random choice in a while loop then you can use such code:

    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)))