vendredi 2 juin 2017

Is there a way to check if multiple statements are true using a single 'elif'?

In response to this challenge, I wrote the following code:

for i in range(1, 101):
    if i % 3 == 0:
        print("Fizz")
    if i % 5 == 0:
        print("Buzz")
    if i % 15 == 0:
        print("Fizzbuzz")
    elif i % 3 !=0:
        print(i)
    elif i % 5 !=0:
        print(i)
    elif i % 15 !=0:
        print(i)

It works, but I'm not sure it is the most efficient method. For example, one of the things I was unsure of is how to include several tests for divisibility using just 1 elif. Any help appreciated!

Aucun commentaire:

Enregistrer un commentaire