Can this be refactored?

[Challenge] The Classic FizzBuzz Challenge

if q%3 == 0 and q%5 == 0:

Is this line really needed?

if q%3 != 0 and q%5 != 0:

I’m writing you privately because discussion in Challenges is better taken to another topic. Once you reply I can make this into a new topic with ties back to the Challenge topic. This way it does not interfere with the flow of that topic and we can extend the discussion, if it comes to that.

Yes, that line is the one that prints the numbers that do not meet the previous condition.

Would not a simple else do the job?

        print q

Something else to consider,

for q in range(1,101):
	if q%3 == 0:
	if q%5 == 0:
	if q%3 == 0 and q%5 == 0:
	if q%3 != 0 and q%5 != 0:

The aboue will print,


for all values divisible by 15. All the branches are exposed and mutually independent.

if ___:

elif ___:

elif ___:


Now each branch is isolated and only one can be followed for any single value.

As for this line,

if q%3 == 0 and q%5 == 0:

it should appear first in the list, and can be simplified,

if q % 15 === 0:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.