tags:

views:

142

answers:

4

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() 
+3  A: 
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), ...].

Daniel DiPaolo
Okay I am trying to print the output of the following code 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) for i in range(n): y = 2.0 * y * (1 - y)print ? main()
fluxus
Added to the original question, and will update my answer as promised
Daniel DiPaolo
If you wanted to print the text in columns, you could use tabs like so:`print 'x\ty' #newlinefor x, y in zip(xs, ys): #newline print str(x) + '\t' + str(y)`
None
+2  A: 
>>>print "a table in python? using two columns"
a table in python? using two columns

;-)

zovision
+1, although you forgot a carriage return.
mawimawi
Edited, thanks :-)
zovision
+1  A: 

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

phasetwenty
A: 

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
Pierce
this was exactly it thanks very much
fluxus