Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop that takes turns between printing one thing or the other python

Tags:

python

Not sure how else to word the title, but this is what I need:

Print either the number zero, the number one or a phrase in no consecutive order twenty times. This is what I have:

    n = 0
    x = “hello”
    for i in range(20):
        print(n, end= ‘’) or print(n+1, end= ‘’) or print(x)

The only problem is that it prints them out in order so that it is always 01hello01hello01hello and so on. I need it to be randomized so that it could print something like 000hello1hello101 or just any random variation of the three variables.

Let me know how :)

like image 936
Iplaysoccer Avatar asked Oct 25 '25 18:10

Iplaysoccer


1 Answers

import random
choices = [1, 0, "hello"]
for i in range(20):
    print(random.choice(choices))
like image 143
Jamie.Sgro Avatar answered Oct 28 '25 07:10

Jamie.Sgro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!