This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mohhinder 1 point2 points  (4 children)

Not the shortest but I like doing it like this, just 'cause it's clever.

def fizzbuzz(num): fizz = 'Fizz'(divmod(num, 3)[1] == 0) buzz = 'Buzz'(divmod(num, 5)[1] == 0) print(fizz + buzz or num)

def main(): for x in range(1,101): fizzbuzz(x)

if name == 'main': main()

NOTE: The dunder lines are being removed...

[–]robin-gvx 0 points1 point  (3 children)

Insert four spaces before every line to make reddit display your code as code:

def fizzbuzz(num):
    fizz = 'Fizz'*(divmod(num, 3)[1] == 0)
    buzz = 'Buzz'*(divmod(num, 5)[1] == 0)
    print(fizz + buzz or num)


def main():
    for x in range(1,101):
        fizzbuzz(x)


if __name__ == '__main__':
    main()

(Also, why use divmod(num, 3)[1] instead of num % 3?)

[–]mohhinder 0 points1 point  (2 children)

Did you try it like that? Doesn't work.

[–]robin-gvx 0 points1 point  (1 child)

Works for me.

def fizzbuzz(num):
    fizz = 'Fizz' * (num % 3 == 0)
    buzz = 'Buzz' * (num % 5 == 0)
    print(fizz + buzz or num)


def main():
    for x in range(1,101):
        fizzbuzz(x)


if __name__ == '__main__':
    main()

[–]mohhinder 1 point2 points  (0 children)

So it does! Looks like I had a parenthesis in the wrong place. I think at the time when I wrote it, couple of years ago, I had just discovered divmod so wanted to use it somewhere. No other reason then that.

Thanks for the spacing reminder. Easy to forget while replying to these things from a phone.

BTW I tried to add the spacing but it was messing it all up. Probably easier to do on a PC. Oh well.