Can this be refactored?

fizzbuzz

#1

[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.


#2

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


#3

#4

Would not a simple else do the job?

    else:
        print q

Something else to consider,

for q in range(1,101):
	if q%3 == 0:
		print('Fizz')
	if q%5 == 0:
		print('Buzz')
	if q%3 == 0 and q%5 == 0:
		print('FizzBuzz')
	if q%3 != 0 and q%5 != 0:
		print(q)

The aboue will print,

Fizz
Buzz
FizzBuzz

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

if ___:

elif ___:

elif ___:

else:

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:

#5

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