I have only just started to learn python as my first language and whilst i worked out the code for fizzbuzz, i cannot for the life of me get it to do the items below. I also want it to print horizontally instead of vertically. Any help would be great (heads spinning). Create a function which does this. For example
fizzbuzz(20)
would print
1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz
def fizzbuzz(n):
for x in range (101):
if x%3==0 and x%5==0:
print("fizz buzz")
elif x%3==0:
print('fizz')
elif x%5==0:
print('buzz')
else:
print (x)
def main():
print(fizzbuzz(20))
Shorter yet:
for n in range(100):
print "Fizz"*(not n % 3) + "Buzz"*(not n % 5) or n
Or, if you want to impress your interviewer,
for n in range(100):
print 'FizzBuzz'[n%-3&4:12&8-(n%-5&4)] or n