DEV Community

Cover image for Python FizzBuzz
UKJP
UKJP

Posted on

2 1

Python FizzBuzz

Note, my new tool kwik is available here

So doing a few fizzbuzz in different languages today and got to python,
Could someone tell me if the following is acceptable in interviews and what their most concise version is:

def fizzbuzz(i):
    n = [i%3, i%5]
    print(((n[0]==0)and(n[1]==0)and"fizzbuzz " + str(i))or((n[0]==0)and"fizz " +str(i))or((n[1]==0)and"buzz " + str(i))or str(i))

for i in range(0,102):
    fizzbuzz(i)
Enter fullscreen mode Exit fullscreen mode

Sam.

Top comments (2)

Collapse
 
jmplourde profile image
Jean-Michel Plourde

The Zen of Python says:

Sparse is better than dense.

While it is clever, I find the print line very hard to read. I would make it more readable and make my point.

Collapse
 
samaldis profile image
UKJP

Thank you,
this was exactly the kind of feedback I needed!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay