0

I have written a code in Python that includes a loop that provides 100 answers pertaining to one variable. So for example the output looks like this when I run it in the shell: (top to bottom)

1   
2  
3     
4  
5   
etc... 

How do I format this to show 10 outputs per line: (left to right)

1  2  3  4  5

Here is my code:

def main():

n = 0

while n <= 100:
    if getPentagonalNumber(n):
        n += 1
        number = (n * (3*n - 1)) / 2
        print (format(number, "5.0f"), end = ' ')


def getPentagonalNumber(n):

    for n in range (1, 100):
        if n > 100:
            return False
    return True

main()
  • possible duplicate of [python print end=' '](http://stackoverflow.com/questions/2456148/python-print-end) – Bhargav Rao Apr 02 '15 at 06:18
  • like `' '.join(['1', '2', '3'])` – Avinash Raj Apr 02 '15 at 06:19
  • Could you please show your code? What did you try? – abarisone Apr 02 '15 at 06:27
  • I tried the solution of using: print(i, end = ' '). It worked, but I need to show 10 outputs per line, how do I do this? – Ryan Combs Apr 02 '15 at 07:02
  • 1
    What are you exactly trying to do in the function `getPentagonalNumber`? It seems to always return `True`. – agamagarwal Apr 02 '15 at 07:10
  • I'm new at functions and this was the only version of code that seemed to work, so I left it alone. Would it work without the False condition? – Ryan Combs Apr 02 '15 at 07:17
  • @RyanCombs: There are a number of logic issues with your code (unrelated to your questions about printing). Your `while` loop will run forever if `getPentagonalNumber` ever returns anything but `True`, since the `n` value is only incremented within the `if` statement. That said, `getPentagonalNumber` isn't a very useful function, since it ignores its argument and simply loops from `1` to `99` and confirms that none of those values are greater than `100`. I suggest using `for` loop on a `range` instead of the `while`, and making `getPentagonalNumber` do something useful. – Blckknght Apr 02 '15 at 08:00

1 Answers1

0

try the following

print(i, end=' ')

EDIT: If you want to print 10 numbers per line, you can do something like this

if i%10==0:
    print('')

This will add a new line after every 10 numbers. (Note: This is assuming that i is the loop variable which is incremented once in every iteration)

agamagarwal
  • 978
  • 7
  • 17
  • That's exactly what I was looking for. I actually need to print 10 answers per line. How would that be approached? – Ryan Combs Apr 02 '15 at 06:49
  • @RyanCombs: That's a different question to what you originally asked! There are a few different ways to print 10 answers per line. Which way is the most suitable depends on your code. So ask a new question, showing us some relevant code so we can see what you're loop is doing. – PM 2Ring Apr 02 '15 at 07:02
  • @RyanCombs I have edited the answer according to your comment. – agamagarwal Apr 02 '15 at 07:07
  • I apologize for the struggle and I appreciate your help. I edited the original question and included my code. – Ryan Combs Apr 02 '15 at 07:08