[Tutor] Print Output Location in Windows XP
Up until now I have been running my Python programs in Linux. I just wrote one in Windows XP and I can't find the print output. The Python program has a print statement, and when I run the program a black window opens and I can see printing going on. However, when it's finished printing, the output window closes. How do I get the output window to stay open, or where is the output stored? Thanks! Tom Strickland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
The problem has been solved. It turned out that I made a newbie mistake that had nothing to do with importing lists. I have a function, sma, which calculates the moving average for a list of prices. I passed the "close" (subsequently changed to "cloze") list to the function as an argument. There is a "for" loop in the function that appends close to a new list.I had written this as: p.append(close) when it should have been p.append(close[i]) This mistake caused the function to append the entire "close" list to "p" instead of just "close[i]" each time through the loop which was more than 4000 times.. This mistake caused a print statement to fill the output screen with numbers. It was difficult to determine that my problem wasn't importing "close", but in how I was using it. Thanks to all who offered suggestions. While my use of "open" and "close" as list names apparently didn't cause any problems, it's bad form and I've changed those two names. Tom ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Re: Importing a List from Module]
Original Message Subject:Re: Importing a List from Module Date: Sun, 28 Aug 2005 16:37:26 -0500 From: Tom Strickland <[EMAIL PROTECTED]> To: tutor@python.org The problem has been solved. It turned out that I made a newbie mistake that had nothing to do with importing lists. I have a function, sma, which calculates the moving average for a list of prices. I passed the "close" (subsequently changed to "cloze") list to the function as an argument. There is a "for" loop in the function that appends close to a new list.I had written this as: p.append(close) when it should have been p.append(close[i]) This mistake caused the function to append the entire "close" list to "p" instead of just "close[i]" each time through the loop which was more than 4000 times.. This mistake caused a print statement to fill the output screen with numbers. It was difficult to determine that my problem wasn't importing "close", but in how I was using it. Thanks to all who offered suggestions. While my use of "open" and "close" as list names apparently didn't cause any problems, it's bad form and I've changed those two names. Tom ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
Tom Strickland wrote: > Eric, > > No, "xy" isn't used anywhere else in the program. It's just a dummy > variable I used to print out "enterData.close". I could easily have > left it out. > > Tom > > > Eric Walker wrote: > >>I am a newbie but do you have anything else named xy >>in your main module. >> >>Eric.. >> >>--- Tom Strickland <[EMAIL PROTECTED]> wrote: >> >> >> >>>I have a module called "enterData" which generates a >>>list, "close" from >>>a data file. "close" is a list of floats. When I put >>>a print statement >>>in that module it will print out an individual >>>member of the list. For >>>example, >>> >>>print close[0] >>> >>> >>>prints the first member of the list. >>> >>>In my "main" module I import "enterData" and try to >>>read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" >>>list, not just the first >>>term. >>> >>>What's my mistake and how do I correct it? >>> >>>Thank you! >>>___ >>>Tutor maillist - Tutor@python.org >>>http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >> >> >> >> >>__ >>Yahoo! Mail for Mobile >>Take Yahoo! Mail with you! Check email on your mobile phone. >>http://mobile.yahoo.com/learn/mail >> >> >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
Byron, I'm confused (as usual). In "def returnList():" that you write below, should the items in the newList list be close[i] and looped to fill "newList" with the contents of "close"? If so, how is "returnLost" different from "close"? Thanks! Tom Byron wrote: > Tom Strickland wrote: > >> In my "main" module I import "enterData" and try to read the first >> element of "close" as follows: >> >> import enterData >> xy=enterData.close >> print xy[0] >> >> When I do this it prints out the entire "close" list, not just the >> first term. > > > > Hi Tom, > > I would create a function in your module that returns the list. > Here's a quick, simplified example: > > def returnList(): > newList = [] > newList += [123.45] > newList += [529.59] > newList += [259.92] > return newList > > aList = returnList() > print aList > > > Note the return statement... This enables assignment, as you have > done in "xy=enterData.returnList()" > > Hope this helps, > > Byron > --- > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importingf a List from Module
Tom Strickland wrote: >>Here are the modules in question: >> >> > This is the main.py module > > #!/usr/bin/python2.4 > import enterData > import movAvg > smavg=[] > xy=enterData.close > print xy[0] > smavg = movAvg.sma(20,enterData.close) > emavg=[] > emavg=movAvg.ema(20,enterData.close) > import stoResults > stoResults.store(enterData.date, enterData.close,smavg,emavg) > print "Finished" > > > ##This is the enterData.py module > ##!/usr/bin/python2.4 > input = open('/home/tom/Python/Input/SPY2.csv', 'r') > s = input > date =[] > open = [] > close = [] > hi = [] > lo = [] > vol = [] > for s in input: > s = s[:-2] > y =[] > y = s.split(',') > date.append(y[0]) > open.append(float(y[1])) > hi.append(float(y[2])) > lo.append(float(y[3])) > close.append(float(y[4])) > vol.append(float(y[5])) > input.close() > for i in range(5): > print close[i] > print 'enterData.py' > > > > > *********** > > > >>-- >> >>Message: 7 >>Date: Sat, 27 Aug 2005 22:27:23 -0500 >>From: Tom Strickland <[EMAIL PROTECTED]> >>Subject: [Tutor] Importing a List from Module >>To: tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>I have a module called "enterData" which generates a list, "close" from >>a data file. "close" is a list of floats. When I put a print statement >>in that module it will print out an individual member of the list. For >>example, >> >>print close[0] >> >> >>prints the first member of the list. >> >>In my "main" module I import "enterData" and try to read the first >>element of "close" as follows: >> >>import enterData >>xy=enterData.close >>print xy[0] >> >> >>When I do this it prints out the entire "close" list, not just the first >>term. >> >>What's my mistake and how do I correct it? >> >>Thank you! >> >> >>-- >> >>Message: 8 >>Date: Sun, 28 Aug 2005 00:15:15 -0400 >>From: Kent Johnson <[EMAIL PROTECTED]> >>Subject: Re: [Tutor] Importing a List from Module >>Cc: tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>Tom Strickland wrote: >> >> >>>I have a module called "enterData" which generates a list, "close" from >>>a data file. "close" is a list of floats. When I put a print statement >>>in that module it will print out an individual member of the list. For >>>example, >>> >>>print close[0] >>> >>> >>>prints the first member of the list. >>> >>>In my "main" module I import "enterData" and try to read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" list, not just the first >>>term. >>> >>>What's my mistake and how do I correct it? >>> >>> >> >>What you have shown here looks fine to me. Can you show some more of >>enterData? >> >>Kent >> >> >> >>-- >> >>Message: 9 >>Date: Sat, 27 Aug 2005 21:25:36 -0700 >>From: Byron <[EMAIL PROTECTED]> >>Subject: Re: [Tutor] Importing a List from Module >>To: Tom Strickland <[EMAIL PROTECTED]>, tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>Tom Strickland wrote: >> >> >>>In my "main" module I import "enterData" and try to read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" list, not just the first >>>term. >>> >>> >> >> >>Hi Tom, >> >>I would create a function in your module that returns the list. Here's >>a quick, simplified example: >> >>def returnList(): >> newList = [] >> newList += [123.45] >> newList += [529.59] >> newList += [259.92] >> return newList >> >>aList = returnList() >>print aList >> >> >>Note the return statement... This enables assignment, as you have done >>in "xy=enterData.returnList()" >> >>Hope this helps, >> >>Byron >>--- >> >> >> >>-- >> >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> >> >>End of Tutor Digest, Vol 18, Issue 106 >>** >> >> >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Importing a List from Module
I have a module called "enterData" which generates a list, "close" from a data file. "close" is a list of floats. When I put a print statement in that module it will print out an individual member of the list. For example, print close[0] prints the first member of the list. In my "main" module I import "enterData" and try to read the first element of "close" as follows: import enterData xy=enterData.close print xy[0] When I do this it prints out the entire "close" list, not just the first term. What's my mistake and how do I correct it? Thank you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
Alan, Now I understand! Thanks again for the explanation! Alan G wrote: >> "for" loop as you suggest, the program won't enter the loop unless >> "s" is initialized so that it's in "input". How do I do that? > > > for s in input: > > means that s takes on each value in input. > input is your file. Thus s takes on the value of each line in > the input file. You don't need to initialise s before entering > the loop as you would with a while loop. Similarly you don't > need to test for the end of the file, 'for' does all that too. > > Take a look at the 'Loops' topic and then the 'Handling Files' topic > in my tutorial for more info on this. > >> Also, near the end of your remarks you say that the code at the >> bottom of my program doesn't do anything. It does for me. > > > Lets take a look: > print N for i in range(N): T[i]< This does nothing print T[i] >>> > > The line that simply has the value in it will not do anything. > It will not print out the value, you need to call print for that > to happen. > >> put in those print statements to verify that the contents of those >> variables are what I expected them to be. That's all that mess is for. > > > The debug/test print statements are fine, I was only pointing out > that one line did nothing, not the entire block. > > HTH, > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
Alan, Thanks for your comments. I see how I can simplify my code, but I've run into a problem trying to do so. If I replace the "while" loop with a "for" loop as you suggest, the program won't enter the loop unless "s" is initialized so that it's in "input". How do I do that? Also, near the end of your remarks you say that the code at the bottom of my program doesn't do anything. It does for me. Since I'm trying to learn Python, I'm not too sure that what I'm doing is correct. Thus, I put in those print statements to verify that the contents of those variables are what I expected them to be. That's all that mess is for. Thanks for your help and Danny's too! Tom Alan G wrote: > Hi Tom, > > Glad you got it working with Danny's help. > > I'll throw in some style points too. > >> input = open('/home/tom/Python/Input/SPY3.txt', 'r') > > >> N=0 >> s = 'boo' >> while s: >>s = input.readline() >>if s == '':break > > > You might find it easier to use a for loop. > > you could for example use > > for s in input: > > > to replace most of the above code and, in the process, eliminate the > need for N completely, see below... > > >>s = s[:-2] >>T[N] = s.split(',') > > > Since you are using append elsewhere why not for T too? > And if you store the split result in a local value before putting it > into T you can use that variable in all the following appends to save > indexing T... > >>date.append(T[N][0]) >>open.append(float(T[N][1])) >>hi.append(float(T[N][2])) >>lo.append(float(T[N][3])) >>close.append(float(T[N][4])) >>vol.append(float(T[N][5])) > > >>N+=1 > > > And with the for loop you don;t need to increment N either. > >> print N >> for i in range(N): > > > And you can use len(T) to replace N here. > >>T[i] > > > This doesn't do anything! :-) > >>print T[i] >>print date[i], open[i], hi[i], lo[i], close[i], vol[i] >> print T[1][2], T[0][0] >> z = (hi[2] +lo[2])/2.0 >> print z > > > HTH, > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]
Danny, Here's the working program. Tom * #!/usr/bin/python2.4 input = open('/home/tom/Python/Input/SPY3.txt', 'r') N=0 s = 'boo' date =[] T = [0,0,0,0,0,0] #don't know why this is necessary open = [] close = [] hi = [] lo = [] vol = [] while s: s = input.readline() if s == '':break s = s[:-2] T[N] = s.split(',') date.append(T[N][0]) open.append(float(T[N][1])) hi.append(float(T[N][2])) lo.append(float(T[N][3])) close.append(float(T[N][4])) vol.append(float(T[N][5])) N+=1 print N for i in range(N): T[i] print T[i] print date[i], open[i], hi[i], lo[i], close[i], vol[i] print T[1][2], T[0][0] z = (hi[2] +lo[2])/2.0 print z *********** Danny Yoo wrote: >On Tue, 23 Aug 2005, Tom Strickland wrote: > > > >>I changed the program in accordance with your advice and it now runs >>perfectly!!! >> >> > >Hi Tom, > >That's very good to hear; glad it's working now. > >If you don't mind, can you post up what you have now? I left out some >other suggestions to the program because they were more about style and >Python idioms. But now that you have a working program, it might be >useful to cover those. > >Good luck! > > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]
Danny, I changed the program in accordance with your advice and it now runs perfectly!!! Thanks for the education! Tom Strickland Danny Yoo wrote: >[Danny] > > >>>Anyway, this doesn't answer the problem: how do we add elements to a >>>list? In Python, we can accumulate elements in a list by append()ing: >>> >>> > >[code cut] > > >[Tom] > > >>So, for example, would I use the following in my "while" loop: >> >> date.append(T[N][0]) >> >>Before the loop, date would have to be defined as date =[], is this >>correct? >> >> > >Yes. Each of the list variables that you're accumulating should each be >initialized to an empty list. > >Good luck! > > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]
Danny, Thanks for your comments and your help. I've added my comments to your text below. Hopefully it will be in red so you can easily identify them. Tom Danny Yoo wrote: >Hi Tom, > >Before we continue: it looks like you're starting to learn Python. Have >you gone through one of the tutorials here? > >http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > >Alan Gauld's tutorial is especially nice, but all of the tutorials there >should be useful. If you go through any one of them, each should help >address the issues you're running into. > > Tom's Comments: I have one of the tutorials and I'm also reading Learning Python by Lutz & Ascher. > >Ok, let's look at the program. I'll try to make constructive criticism >along the way. > > > >>input = open('/home/tom/Python/Input/SPY3.txt', 'r') >>N=0 >>s = 'boo' >>date =['hoo'] >>T = [0,0,0,0,0,0] #don't know why this is necessary >> >> > >There are an awful number of global variables here, and their names do not >make it clear what the role of each variable is. What is 's', and what is >'T'? And why does date contain the string 'hoo'? > > Tom's Comments: N is the counter which I initialize here. s is a string and I've intiialized it to 'boo' so that it passed the first "while" test and entered the loop. The "date" variable is defined just to see if it made any difference. I had previously not defined it, and the program hung up on the "date" assignment so I tried assigning it a value but it didn't help. T is the matrix consisting of several rows of six columns each. If I leave out this definition of T =[0,0,0,0,0,0] the program hangs up on the line: T[N] = s.split(',') >Also, the original code was double-spaced; you don't need to do that. >You're not writing a report that's graded based on filling 80 lines, are >you? *grin* > > Tom's Comments: The original code was written using Eric. In going from Eric to Word to Thunderbird e-mail the double space crept in. It's not in the original. Also, the line numbers were lost along the way. > > > > >>open = close = hi = lo = vol = [1.0] #is this necessary? >> >> > >This looks highly suspicious. > > Tom's Comments: I just put this in to see if it would help. It didn't. I thought it might since Python didn't like for me to use T without first assigning it some values. >In Python, names are just references to things. That means that all of >these names --- open, close, hi, low, vol --- are just aliases for the >same list thing. For example: > >## > > maui = hawaiian = ['pizza'] hawaiian[0] = 'ham and pineapple pizza' hawaiian >['ham and pineapple pizza'] > > maui >['ham and pineapple pizza'] >## > >Notice that because 'maui' and 'hawaiian' are refering to the same list, >when we change the list, we see that reflected here in both 'maui' and >'hawaiian'. > > Tom's Comments: Good point! I didn't realize this. > > > > >>while s: >>s = input.readline() >>if s == '':break >>s = s[:-2] >>T[N] = s.split(',') >>date[N] = T[N][0] >>open[N] = T[N][1] >>hi[N] = T[N][2] >>lo[N] = T[N][3] >>close[N] = T[N][4] >>vol[N] = T[N][5] >>N+=1 >> >> > >Ok, there's a problem here. > >One thing you may need to know is that Python's lists do not automatically >expand as we try to enter elements into them. For example: > >## > > names = ['knuth', 'morris'] names[2] = 'pratt' >Traceback (most recent call last): > File "", line 1, in ? >IndexError: list assignment index out of range >## > >This breaks because the list is defined contain two elements: trying to >assign to names[2] goes out of the list bounds, and that's an error in >Python. In practice, this "array out of bounds" check is often a Very >Good Thing because index errors can be evidence of a logical program >error. > > >Anyway, this doesn't answer the problem: how do we add elements to a list? >In Python, we can accumulate elements in a list by append()ing: > >## > > names = [] names.append('john') names.append('lisa') names.append('erik') names.append('tina') names >['john', 'lisa', 'erik', 'tina'] >## > >Does this make sense? > > Tom's Comments: Yes! So, for example, would I use the following in my "while" loop: date.append(T[N][0]) Before the loop, date would have to be defined as date =[], is this correct? > > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need Help on Assignment
I have imported a text file consisting of lines of csv. Each line consists of six items separated by commas. I have converted this file into a matrix of string variables, T[i]. Now I want to extract each of the individual strings, convert five them to floats, and save them in individual vectors. That is, I'd like to convert each T[i] into date[i] (a string), open[i] (a float) etc. Python doesn't like the ways I've attempted to make these assignments. T[i], which is a six-column matrix, works fine. However, if I say date[i] = T[i][0], it doesn't like this. I've tried several variations on this but with no success. Things like: [date[i],open[i], etc] = T[i] didn't work either. What do I need to do to make this type of assignment? Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor