fluxus
fluxus

Reputation: 11

How do i print a table in python?

I am trying to print the output of the following code in two columns using the python launcher:

def main():
    print "This program illustrates a chaotic function"
    n = input("How many numbers should I print? ")
    x = input("Enter a numbers between 0 and 1: ")
    y = input("Enter another number between 0 and 1: ")
    for i in range(n):
        x = 2.0 * x * (1 - x)
        print #??
    for i in range(n):
        y = 2.0 * y * (1 - y)
        print #??

main() 

Upvotes: 0

Views: 8260

Answers (4)

Pierce
Pierce

Reputation: 346

If all you want is an x and a y value on each line, then once the preliminaries are done, you can say:

for i in range(n):
    x = 2 * x * (1 - x)
    y = 2 * y * (1 - y)
    print x,y

Upvotes: 1

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

for x, y in listOfTwotuples:
    print x, y

Given that you've provided no details I've gone ahead and assumed that you've got a list of two-tuples. Update your question with more info and I'll update my answer to match!

edit: with actual details now

If in each loop you store the numbers in a list, you can then use zip to get the format needed to use my code snippet above.

So after reading the input in (be careful with input by the way, using raw_input is better, google why):

xs = []
ys = []
for i in range(n):
    xs.append(2.0 * x * (1 - x))
for i in range(n):
    ys.append(2.0 * y * (1 - y))

Then you can use zip to apply my code snippet above:

for x, y in zip(xs, ys):
    print x, y

zip takes one list [0, 1, 2, ...] and another [10, 20, 30, ...] to produce a list of tuples with these lists [(0, 10), (1, 20), (2, 30), ...].

Upvotes: 3

Chris
Chris

Reputation: 3509

Check out the format string syntax which will help you to pad strings with spaces to get columns.

Upvotes: 1

zovision
zovision

Reputation: 942

>>>print "a table in python? using two columns"
a table in python? using two columns

;-)

Upvotes: 2

Related Questions