-1

I currently have

for i in range (1,26,1):
    for e in range (1,7,1):
        print i**e

I am supposed to get the outcome:

1 1 1 1 1  
2 4 8 16 32  
3 9 27 81 243  
…  
25 625 15625 390625 9765625

by nesting a for statement within a for statement, but when I run this code every number ends up on its own line. How do I get it to format like the above example?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
G.Bow
  • 1

2 Answers2

0

Use sys.stdout.write.

import sys
sys.stdout.write("Hello World!")
sys.stdout.write("Jane Doe")

should print

Hello World!JaneDoe

Don't forget to print out the line breaks! Line breaks are coded with \n

>> sys.stdout.write("Hello World!\n")
Hello World!
>>
Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45
0

Python 3 Solution:

for i in range (1,26,1):
    for e in range (1,7,1):
        print( i**e, end=" " )
    print( "" )

Python 2 Solution:

for i in range(1,26,1):
    row = ""                   
    for e in range(1,7,1):
        row += str(i**e) + " "
    print(row)
David C
  • 203
  • 3
  • 7
  • This solution only works in python 3, it does not work in python 2.7 unless you import from \_\_future__. In general, you do not want to import from \_\_future__. The OP put a python 2.7 tag in the question. – Paul Terwilliger Sep 13 '16 at 17:56