3

I need to print this code:

for x in range (1, 21):  

    if x%15==0:
        print("fizzbuzz")

    elif x%5==0:
        print("buzz") 
    elif x%3==0:
        print("fizz")

    else:
        print (x)

Horizontally instead of it printing vertically, like this:

1

2

3

to

1 2 3

I am not sure how to, some help would be great. Thanks

Nick T
  • 25,754
  • 12
  • 83
  • 121
Jessica Smith
  • 45
  • 1
  • 2
  • 7

4 Answers4

10

Two options:

Accumulate a result string and print that at the end:

result = ""  
for x in range (1, 21):  

    if x%15==0:
        result = result + "fizzbuzz "

    etc...
print result

Or tell Python not to end the printed string with a newline character. In Python 3, which you seem to be using, you do this by setting the end argument of the print function, which is "\n" (a newline) by default:

for x in range (1, 21):  

    if x%15==0:
        print("fizzbuzz",end=" ")

    etc...

Historical note: In Python 2, this could be achieved by adding a comma at the end of the print statement; print "fizzbuzz",

Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • 1
    I'm not the downvoter; in fact I just upvoted, because I believe the OP is using Python 3, not 2 (given the way he wrote his print functions/statements). And if he's using Python 3, then this answer is the best one. – rmunn Aug 27 '13 at 06:15
  • 1
    I modified it and it works great with the end=" " thanks very much for that! – Jessica Smith Aug 27 '13 at 06:23
4

do this and it will print all on one line:

for x in range (1, 21):  

    if x%15==0:
        print ("fizzbuzz"),

    elif x%5==0:
        print ("buzz"), 
    elif x%3==0:
        print ("fizz"),

    else:
        print (x),

it will print like this:

1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz

Serial
  • 7,925
  • 13
  • 52
  • 71
  • 4
    I believe the OP is using Python 3, not 2, from looking at his use of print functions rather than print statements -- in which case this answer will be incorrect. If he is using Python 3, the `end=" "` approach in the other answer is better. – rmunn Aug 27 '13 at 06:13
  • I am using 3 yes, the result of what Christian has said still prints vertically. – Jessica Smith Aug 27 '13 at 06:18
  • Ohhhh very sorry im not using 3 but i will keep my asnwer incase someone stumbles upon this question and is using 2 sorry about that :) – Serial Aug 27 '13 at 06:19
  • No worries, I got the code to work with the end=" " thanks for the tutorial for 2 though =) – Jessica Smith Aug 27 '13 at 06:22
2

A '\n' character is written at the end, unless the print statement ends with a comma.

http://docs.python.org/2/reference/simple_stmts.html

Takahiro
  • 1,222
  • 10
  • 11
  • This is for Python2, the original question targets Python 3 where you get that effect with using `print( ... , end='')`, effectively redefining the "end of string" to be empty instead of the default `\n` – cfi Aug 27 '13 at 11:43
0

You can try this, it tells each line what to end with and separates it with a coma for easier reading:

for x in range (1, 21):
    if x%15==0:
        print("fizzbuzz", end = ", ") 
    elif x%5==0:
        print("buzz", end = ", ")

    elif x%3==0:
        print("fizz", end = ", ")
    
    else: 
        print (x,  end = ", ")