3 ms·
import sys n = 1000 modthree = 0 modfive = 0 for i in range(n): if modthree == 3: modthree = 0; sys.stdout.write("fizz") if modf
by madcaptenor 13y ago
import sys
n = 1000
modthree = 0
modfive = 0
for i in range(n):
if modthree == 3:
modthree = 0; sys.stdout.write("fizz")
if modfive == 5:
modfive = 0; sys.stdout.write("buzz")
if modthree and modfive:
sys.stdout.write(i)
sys.stdout.write("\n")
modthree += 1; modfive += 1
- smtddr 13y agoThat sys.stdout.write usage is interesting. Any reason for not just using print? Or if you want all of them on the same line, you can put a comma at the end of print, like... for q in range(10): print q,
- GlitchMr 13y agoThis is Python 2, not Python 3.
- madcaptenor 13y agoI wanted to get everything on the same line; didn't realize the comma would do that.
- smtddr 13y agoUnderstood. Now that I think of it, I did the same thing until someone told me about the comma. Still though, the comma-trick puts spaces in between the numbers. I don't know how to get rid of that.
- michaelhoffman 13y agoYou can hack the internals like this: print q, sys.stdout.softspace = 0 But really, this wart is one of the reasons Python 3 has a new way of printing.