Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For loop that takes turns between printing one thing or the other 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Let me know how 🙂

>Solution :

import random
choices = [1, 0, "hello"]
for i in range(20):
    print(random.choice(choices))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading