-3

I need for this code to print horizontally and I've looked around but none of the answers seem to work for me.

for x in range (1,20): 
    if x % 5 == 0 and x % 3 == 0:
            print ('FizzBuzz')            
    elif x % 3 == 0: 
        print ('Fizz')

    elif x % 5 == 0: 
        print ('Buzz')        
    else: 
        print (x)    .
sberry
  • 128,281
  • 18
  • 138
  • 165
  • Show us what you've looked at, and in what way they didn't "seem to work". Because there are lots of perfectly good answers out there that work perfectly well, and we have no idea which ones you looked at and what was wrong with them, so we can't help you. – abarnert Aug 11 '14 at 06:02
  • Meanwhile, the most obvious problem in your code is `print (x) .`, That stray `.` will raise a `SyntaxError` before anything runs. Also, the haphazard mix of indentation implies that you might be mixing spaces and tabs, which could easily lead to either an `IndentationError`, or the code running but not doing what you expect it to. – abarnert Aug 11 '14 at 06:03
  • 1
    Oh my god, here it is, an example of someone who can't solve FizzBuzz. And I thought it was a joke! – Patrick Collins Aug 11 '14 at 06:03
  • @sberry: Not if he's using Python 3. (And if he's using Python 2, the parentheses around the `print` arguments are unnecessary and misleading.) – abarnert Aug 11 '14 at 06:05
  • Guys - relax, he **can** and did solve fizzbuzz - his question was how to print it horizontally - the problem is that every `print` also prints a newline! – Nir Alfasi Aug 11 '14 at 06:05
  • Meanwhile, when I try each of these `print` calls, they all print horizontally. Perhaps you've got a 1-character-wide terminal, or you've set your monitor on its side? – abarnert Aug 11 '14 at 06:05
  • I have looked at the comments in this thread http://stackoverflow.com/questions/18458024/cant-figure-out-how-to-print-horizontally-in-python and my end result was still print vertically – user3928439 Aug 11 '14 at 06:05
  • OK, so you copied the code out of Jessica Smith's question, which has a problem, instead of copying the code out of any of the answers there, which do not, and now you want to know why you have the same problem as Jessica? – abarnert Aug 11 '14 at 06:06
  • @abamet: being stuck on Python2, I often wonder about those types of differences. I am never sure whether someone is on one or the other unless they tag it as such. As far as parens being misleading, I agree mostly, but I am seeing it more and more these days. – sberry Aug 11 '14 at 06:06
  • @sberry: I'm mostly seeing it on code from people who copy someone else's Python 3 code and try to use it in Python 2, or who refuse to say after being asked 3 times whether they're on 2 or 3, get an answer for 3, and then use it in 2… – abarnert Aug 11 '14 at 06:08
  • 2
    Why would anyone upvote a question which is just "I copied this code (incorrectly) from another question and now have the same problem as that copied code"? – abarnert Aug 11 '14 at 06:08
  • @abarnert: lolz, and come to think of it, you are probably right. – sberry Aug 11 '14 at 06:09
  • What's the correct reason to vote to close here? I want "lacks minimal effort"... – Patrick Collins Aug 11 '14 at 06:09
  • @abarnert oh come on - they've changed the `range` and introduced a superfluous `.` :p – Jon Clements Aug 11 '14 at 06:10
  • I wrote this code out by myself and it works properly and I only searched the web after I couldn't figure out how to print horizontally – user3928439 Aug 11 '14 at 06:10
  • @PatrickCollins: I think at this point it's a dup, since he has literally copied and pasted the code from the other question he linked to (http://stackoverflow.com/questions/18458024/cant-figure-out-how-to-print-horizontally-in-python), except for introducing some new typos, and has exactly the same problem as that code. How much more dup can you get? – abarnert Aug 11 '14 at 06:11
  • @abarnert I tend to agree... I'm not quite sure what more can be done here... – Jon Clements Aug 11 '14 at 06:11
  • 1
    I really, really hope that he hasn't successfully talked his way through a job interview because of this thread. – Patrick Collins Aug 11 '14 at 06:13

4 Answers4

0

Change print statements to

print ('FizzBuzz', end=' ') or print ('FizzBuzz'), ==> python 3 syntax

print 'FizzBuzz', ==> python 2.* syntax

rajpy
  • 2,436
  • 5
  • 29
  • 43
-1

You can use sys module:

import sys 
for x in range (1,20): 
    if x % 5 == 0 and x % 3 == 0:
        sys.stdout.write('FizzBuzz ')            
    elif x % 3 == 0: 
        sys.stdout.write('Fizz ')

    elif x % 5 == 0: 
        sys.stdout.write('Buzz ')        
    else: 
        sys.stdout.write(str(x)+' ')    
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
-1

If I understand you correctly (which is hard) I guess you meant that you want to print without newline:

import sys
sys.stdout.write('.')

or (in case you are using python 2)

print('Buzz'),

and in case you are using python 3 (https://docs.python.org/release/3.0.1/whatsnew/3.0.html):

print('Buzz', end="")

Here is an answer: How to print without newline or space?

Community
  • 1
  • 1
zenpoy
  • 19,490
  • 9
  • 60
  • 87
-1

Well I think this is what your looking for:

print('hello world', end='')
print('. And some more on the same line', end='')

Doc: https://docs.python.org/2/library/functions.html#print

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147