Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (50.2k points)

I am currently working on the code for fizzbuzz. In that, I want to print horizontally instead of vertically. And I want to perform that in a user-defined function:


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:


        elif x%5==0:



            print (x)    

def main():


1 Answer

0 votes
by (108k points)

First, try to store all the elements into a list. Then print the list at the end of the method. You can join the items together with a comma in between using ', '.join(...).

def fizzbuzz(n):

    result = []

    for x in range(1, n+1):

        if x % 3 == 0 and x % 5 == 0:

            result.append("fizz buzz")

        elif x % 3 == 0:


        elif x % 5 == 0:




    return result

def main():

    print(', '.join(fizzbuzz(20)))


In Python, we have one attribute named end in the print statement that will help you to print your elements horizontally with some space in the end. And I think, with that, it will nearly solve your problem except that the very last item would also have a comma and space at the end, which is not what you desire.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Browse Categories