0

I´ve been trying to print 2 vectors in a table-like format. The trouble comes when elements from list A and list B don´t have the same ammount of floats

import math
def f(x):
    y=1/(math.sqrt(1+x))
    return y
xmin=0.0
xmax=1.0
print "Write some n:"
n=input()
x=[]
delta=((xmax-xmin)/n)
for i in range (n):
    xx=xmin+i*delta
    x.append(xx)
y=[]
b=f(x[0])
y.append(b)
for i in range (1,n-1):
    yy=2*f(x[i])
    y.append(yy)
y.append(f(x[n-1]))
for i in range(n):
    print x[i],y[i]

The problems comes when printing the last line: What I want:

0.0     1.0

0.125   1.88561808316

0.25    1.788854382

What I get:

0.0 1.0

0.125 1.88561808316

0.25 1.788854382

How can I fix those digits, so the program prints the result properly? Thanks :)

  • print x, " ", y, with the amount of spaces between quotes – postoronnim Sep 27 '18 at 21:44
  • I tried adding the spaces, but it was pretty much the same due to digits. And thank you so much for the link, I tried searching in a lot of ways how to fix this, but missed that link. – Daniel Garcia Sep 27 '18 at 21:52

1 Answers1

1

Use string formatting to do that. The following would set the padding to 6 places:

print(”{:6}{:6}".format(x[i ], y[I]))
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
Joshua Pierce
  • 136
  • 1
  • 5
  • Appreciate the edit Alex. I couldn't get the formatting to work on my phone, I had planned on editing it when I got to my computer. – Joshua Pierce Sep 28 '18 at 00:09