[Tutor] String Attribute
Hi Everyone: What is the source of the syntax error to the String Attribute? Go to the following URL links and view a copy of the raw data file code and sample data: 1.) http://tinyurl.com/p2xxxhl 2.) http://tinyurl.com/nclg6pq Here is the desired output: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Root and power
How is it going tutors? The following problem seems impossible to me: "*Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect*." I would like to solve this problem myself so please don't give me the solution. I need to learn how in the world do find the root and power of an integer that x user entered? I haven been looking on the python website for an appropriate function but I have not. If you have the time can you please tell me about the functions and other facts I need to know in order to solve this problem? Is there a book you guys recommend for total beginners who have no ideal of what computer science and programming is? Thank you, Job ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] line error on no. 7
Thanks, I’ll need some time to review your notes Sent from Surface From: Martin A. Brown Sent: Tuesday, July 28, 2015 4:41 PM To: ltc.hots...@gmail.com Cc: Tutor@python.org Hello again, > The raw python code is located at http://tinyurl.com/oua9uqx It is not very long, so you can post inline (I pasted it below). If you have a longer piece of code, then, yes, a pastebin is a good choice. (Also, if in the future, you have a more complex piece of code, try to simplify your posting to just the part that is giving you trouble.) But, your question is clear here. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: if fh == list: continue list.split() list.append sorted("fh") print line.rstrip() You have confused your names in line 7. list.split() There are a few lessons to learn here. * Are you operating on the variable you intend to be operating on? No, in this case. You wanted to do something like 'lst.split()' * Did you want to throw away the result of the call to .split()? I'm guessing the answer is 'No.' So, you'll need another variable to hold the value from lst.split(). Line 7 should become: lst.extend(line.split()) Additional comments: * Your line 6 performs a test to see if fh (file object referring to the stanza) equals the builtin called 'list'. That doesn't make sense. Try typing list and list() at an interactive prompt, and you may see that it doesn't make sense to compare those things. >>> list >>> list() [] The first tells you what 'list' is. The second calls 'list()', which returns you, well... a new Python list object. * Your line 8 is a NOOP (no-operation). In that line, you are simply referring to a method on the builtin list type. Use the interactive interpreter to see what I mean: >>> * Your line 9 doesn't make much sense to me. It may no longer be a syntax error, but it isn't doing what you think it's doing. Try it out in the interactive interpreter to see what it's doing: >>> sorted("fh") ['f', 'h'] OK, so now, let me make a few suggestions. Others may have additional comments, but I'd be interested in seeing how you adjust your program after working through the above. Thus, I suggest removing most of the loop internals and rewriting. Everything else is a good start. I'll suggest a possible ending, too. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: # Code is good until this point. # Here, you want to find the words and add them to the list # called 'lst'. # -- now, outside the loop, I would suggest printing the list print lst Once you can run your program and get something other than an empty list, you know you have made progress. Good luck with your iambic pentameter, -Martin > The sample data is located at > http://tinyurl.com/odt9nhe Also, I'm including your short data sample: But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Abs
On 29/07/15 04:28, Job Hernandez wrote: How is it going tutors? You only sent it to me. Please use Reply All to include the list. The following problem seems impossible to me: "*Write a program that asks the user to enter an integer and prints two integers, /root /and /pwr/, such that 0 < pwr < 6 and root^pwr (root**pwr) is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect*." I would like to solve this problem myself so please don't give me the solution. I need to learn how in the world do find the root and power of an integer that x user entered? I haven been looking on the python website for an appropriate function but I have not. The only function you need is pow() Or you could do it without a function by using the ** operator. You want to try various integer values and see if the result is the users input. That means you need a loop. The pwr value is set between 1 and 5 in the assignment. The maximum root value will be the user's input (since X**1 = X, and that will always be a valid solution!) Is there a book you guys recommend for total beginners who have no idea of what computer science and programming is? Being biased, I'd recommend my web site(see below). You can get an older version in a paper book if you must, and its mostly still applicable. There is also a good option by Allen Downey which focuses on the CS side of things if that's what you want. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
On 29/07/15 00:33, ltc.hots...@gmail.com wrote: Hi Everyone: What is the source of the syntax error to the String Attribute? Normally I'd ask you to post the full text of any errors. They usually contain a lot of useful information. They also help us identify which syntax error you are asking about in the case where there are several! :-) But in your case it seems you are running the code in an online debugger so you may not have seen the full error text. Although, even there, it gives more information that you posted, namely: AttributeError: 'str' object has no attribute 'startwith' So it's not a syntax error but an attribute error... Your error is with the attribute startwith, which doesn't exist. To check the attributes of a string, type dir(str) at a >>> prompt. (I assume you have access to one of those somewhere?) You will see that you mis-spelled startswith. However, your code has several other problems... Go to the following URL links and view a copy of the raw data file code and sample data: 1.) http://tinyurl.com/p2xxxhl 2.) http://tinyurl.com/nclg6pq If its short (<100 lines?) just include the code in the message. Here it is: count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startwith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" You set the filename and then iterate over the name. I suspect you intended to iterate over the file contents? To do that you need to open the file (which you do near the end!) So something like: with open(fname as in_file: for line in in_file: # do your stuff here The next problem is that the last line of the loop holds the individual elements of the split, but you throw that away when the loop goes back to the top. You need to save the result somewhere so you can process it after the loop completes. For this specific example you could just indent the count = count + 1 print len(line) lines inside the loop. But that won't be enough to get you to your final output of the email addresses. Here is the desired output: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Abs
On 29/07/15 09:51, Alan Gauld wrote: On 29/07/15 04:28, Job Hernandez wrote: How is it going tutors? You only sent it to me. Please use Reply All to include the list. My mistake, you sent it to the list too. For some reason my mailer didn't show the tutor header... Its in a new thread now. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Root and power
On 29/07/15 04:29, Job Hernandez wrote: How is it going tutors? The following problem seems impossible to me: I made a reply in the thread 'Abs' started on July 27th. Basically you can use the pow() function. The power lies between 1-5. The largest root will be the same as the user input since X**1 = X. So you always have at least 1 answer! You need to iterate up to the power of 1 solution to find if there is a smaller pair of integers. Some may have several, for example 16 yields: 16**1 4**2 2**4 Your assignment doesn't make it clear how that should be handled... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Root and power
2015-07-29 5:29 GMT+02:00 Job Hernandez : > How is it going tutors? > > The following problem seems impossible to me: > > "*Write a program that asks the user to enter an integer and prints two > integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is > equal to the integer entered by the user. If no such pair of integers > exists, it should print a message to that effect*." > > I would like to solve this problem myself so please don't give me the > solution. > > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. > > If you have the time can you please tell me about the functions and other > facts I need to know in order to solve this problem? > > Is there a book you guys recommend for total beginners who have no ideal of > what computer science and programming is? > > Thank you, > > Job > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hello, First, you need an algorithm that solves your problem. Once you have it, you need to implement it in Python. For the algorithm. Although there are theorems and all sort of smart mathematical tricks you could use, given the conditions you have, have you considered to use a brute force approach? I mean: if all involved numbers are positive you could start testing different values for root from 0 on, and for each value test pwr from 1 to 5 until you find either a solution, something bigger than x. Once you chose the algorithm, for the actual implementation you have to say what part you are blocked at. Best. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] I need help with my homework. No, really....
I'm taking a beginning Python course at Austin Community College. I'm also taking two other project-based web programming courses. It's summer semester, meaning we have eight weeks instead of the usual 16 to finish all the requirements. The semester ends Friday, July 131st. Yes, I am aware that I'm a teensy bit screwed. I have to complete eight programs ("complete" meaning "functioning"). I'm having a really tough time with this one. It's matrix arithmetic using 2d arrays. If ANYONE can help me, I'd really appreciate it. Someday, maybe I can be the guy helping someone else...except I'm a girl. Whatever. I digress. I'm already planning to retake the course because I want to get more out of it- I like Python a lot, it's just really difficult to absorb it all that fast...especially since I'm used to HTML and JavaScript. Okay- so these are the directions for the program, and I'll cut and paste my program so far from the interpreter to the email. Don't want to freak anyone out with attachments. It's gonna be a long email. Given x as an array of [5,3] and y as an array of [3,7] perform the following: 1. Load array x column-wise and array y row-wise 2. Multiply x by y to compute array z 3. Compute the sum of all elements in column 2 of array x and add it to the sum of all elements in row 2 of y (the first row/column is 0, the second is 1, etc. That got me at first) 4. Compute the smallest element in row 1 of y ---using appropriate headings: 5. Print out matrices x, y, and z (display on screen, but y'all probably get that) 6. Print out sum and smallest element The data with which array x is loaded: 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 The data with which array y is loaded: 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 Must use functions named as follows: LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA lab5.dat is simply a dat file with the data with which the arrays are loaded in one long line, each separated by commas. Thanks- in advance- no more comments after the program. This is what I have thus far: #Lab #5 #COSC 1336-31493 #SUM 2015 NRG #Tu/Th 1:15-4:25pm def main(): #matrix initialization x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] #file declaration infile = open('lab5x.dat','r') infile = open('lab5y.dat','r') outfile = open('lab5.out', 'w') #variables sumx = 0 sumy = 0 small = 0 A = 0 B = 0 C = 0 #call functions LOADX(infile, A) LOADY(infile, B) COMPUTEZ(A, B, C) SUMMATION(A, B) SMALLEST(A) OUTDATA(file, A, B, C) #close files infile.close() infile.close() outfile.close() dummy = input('Press any key to continue.') #develop functions #load matrix x def LOADX(infile, A): #local variables n=0 k=0 s=0 templist = infile.readline().strip('\n').split(',') while (k<3): j=0 while(j<5): A[j][k] = int(templist[n]) s=s+A[j][k] j=j+1 k=k+1 n=n+1 #load matrix y def LOADY(infile, B): #local variables n=0 j=0 templist = infile.readline().strip('\n').split(',') while (j<3): k=0 while (k<7): B[j][k] = int(templist[n]) s=s+B[j][k] j=j+1 n=n+1 k=k+1 #define computation of Z matrix def COMPUTEZ (A, B, C): i=0 while (i<5): j=0 while (j<=7): k=0 while (k<=3): C[i][j]= C[i][j]+ A[i][k] * B[k][j] k=k+1 j=j+1 i=i+1 #def summation def SUMMATION(x,y): s=0 k=0 j=0 while (k<5): sumx=sumx + x[k][2] k=k+1 while (j<7): sumy=sumy + y[2][j] j=j+1 s=sumx + sumy #def smallest def SMALLEST (B): k=0 s=B[1][k] k=k+1 while (k<7): if(s> B[1][k]): s=B[1][k] k=k+1 def OUTDATA(outfile, x, y, z,SMALLEST,SUMMATION): i=0 j=0 k=0 while (k<3): print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4]) k=k+1 file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])] while (j<7): print(B[j][0],B[j][1],B[j][2]) j=j+1 file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])] while (i<7): print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4]) file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])] print ('Summation= ',SUMMATION) file.write('Summation= ', SUMMATION) print ('Smallest= ',SMALLEST) file.write('Smallest= ',SMALLEST) main() ___
Re: [Tutor] I need help with my homework. No, really....
On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > I'm taking a beginning Python course at Austin Community College. I'm also > taking two other project-based web programming courses. It's summer > semester, meaning we have eight weeks instead of the usual 16 to finish all > the requirements. > The semester ends Friday, July 131st. July 131st? Whew, you've got over 100 days to complete this! *wink* But seriously... more comments (hopefully useful comments this time) follow below, interleaved with your code. Grab a coffee, this may be a bit long. Oh, and I'm going to split my reply over a couple of emails. > Yes, I am aware that I'm a teensy bit screwed. > > I have to complete eight programs ("complete" meaning "functioning"). I'm > having a really tough time with this one. It's matrix arithmetic using 2d > arrays. [...] > Given x as an array of [5,3] and y as an array of [3,7] perform the > following: > > 1. Load array x column-wise and array y row-wise I'm not sure that I understand what this means. I think what they mean is that if the data looks like this: 10, 20, 30, 40, 50, 60 and x and y are both 3x2 arrays, we end up with these: # read data down the columns first x = [ [10, 40], [20, 50], [30, 60] ] # read data across the rows first y = [ [10, 20], [30, 40], [50, 60] ] > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. Below, you have lab5x.dat and lab5y.dat. Are there two files, or just one? That's going to make a big difference to the way you read the input. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #Lab #5 > #COSC 1336-31493 > #SUM 2015 NRG > #Tu/Th 1:15-4:25pm > > def main(): > #matrix initialization > x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] > y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] You can simplify the matrix initialization a little bit by using list multiplication: x = [ [0]*3, [0]*3, [0]*3 ] and similarly for y, and z. What they do should be quite obvious: [0]*2 --> [0, 0] ['hello']*3 --> ['hello', 'hello', 'hello'] Now, if you're paying attention, you might think "Wait, why don't I multiply each row as well?" [ [0]*3 ]*5 # Don't do this! I don't want to spend to much time on this, but in a nutshell, the above looks like it should work, but it doesn't work as you would expect because it doesn't copy the inner list. Instead of getting five different rows of [0, 0, 0], you get the same row repeated five times. If my explanation doesn't make sense to you, feel free to ask, or feel free to just accept it on faith that [ [0]*3 ]*5 will not work the way you want. You can always come back to discuss this later. > z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] Your indentation here got messed up. Unfortunately sometimes email doesn't work well with indentation, which is sad. To fix this, you need to indent the line z = ... so that it in aligned with the other lines inside the main function. z = [ [0]*7, [0]*7, etc. ] > #file declaration > infile = open('lab5x.dat','r') > infile = open('lab5y.dat','r') > outfile = open('lab5.out', 'w') You have two variables both called "infile", that isn't going to work. You need to give them separate names, say, infileX and infileY. > #variables > sumx = 0 > sumy = 0 > small = 0 > A = 0 > B = 0 > C = 0 I'm not sure that you need these A B C variables. I think you actually want to use x, y, z, the three matrices you already initialized. > #call functions > LOADX(infile, A) > LOADY(infile, B) > COMPUTEZ(A, B, C) > SUMMATION(A, B) > SMALLEST(A) > OUTDATA(file, A, B, C) That will become: LOADX(infileX, x) LOADY(infileY, y) COMPUTEZ(x, y, z) SUMMATION(x, y) SMALLEST(x) OUTDATA(outfile, x, y, z) > #close files > infile.close() > infile.close() > outfile.close() > dummy = input('Press any key to continue.') Don't forget to change the names of those infiles. More to follow in my next email. -- Steve _
Re: [Tutor] I need help with my homework. No, really....
Part 2... On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > Given x as an array of [5,3] and y as an array of [3,7] perform the > following: > > 1. Load array x column-wise and array y row-wise > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #load matrix x > def LOADX(infile, A): >#local variables >n=0 >k=0 >s=0 It's hard to tell what those variables mean from the names. It may be more useful to give them descriptive names. I think that k is the column number, j (below) is the row number, n is an index into the templist you generate next, and s is, well, I have no idea what s is. You do some calculations on s, but then it never gets used, so I'm not sure what it is for. item = 0 column = 0 s = 0 # huh? >templist = infile.readline().strip('\n').split(',') To be clear, this reads the first line from the file, and one line only. It removes the newline \n from the end, then splits on commas, and returns a list of strings, say: ['1', '2', '3', '4', ...] Is that what you expect? >while (k<3): >j=0 >while(j<5): > A[j][k] = int(templist[n]) > s=s+A[j][k] > j=j+1 > k=k+1 > n=n+1 Assuming s in not needed, this becomes: while (column < 3): row = 0 while(row < 5): A[row][column] = int(templist[item]) row = row + 1 column = column + 1 item = item + 1 But that can't be right, because you end up processing: column=0, row=0 column=1, row=1 column=2, row=2 and then stopping. That only gives you three numbers. What you need is to process fifteen numbers: column=0, row=0 column=0, row=1 column=0, row=2 column=0, row=3 column=0, row=4 column=1, row=0 ... column=2, row=4 The way to do that is to only increase the column when you've processed all the rows. Here's a sketch, you can fill in the details: while (column < 3): while(row < 5): process one element A[row][column] add one to row # when we get here (outdented), we've finished the inner # while loop, but are still inside the outer while loop add one to column > #load matrix y > def LOADY(infile, B): LOADY should be almost exactly the same as LOADX, except that instead of looping down the columns, you should loop across the rows. So: while row < 3: while column < 7: but otherwise more or less the same as LOADX. > #define computation of Z matrix > def COMPUTEZ (A, B, C): Try re-writing COMPUTEZ with row and columns, as above, and see if that makes sense. I can see one obvious problem below: >i=0 >while (i<5): >j=0 >while (j<=7): >k=0 >while (k<=3): >C[i][j]= C[i][j]+ A[i][k] * B[k][j] >k=k+1 This bit can't work, because you have a while loop where k never advances! while k <= 3: process C[i][j] ... but k doesn't change. So Python will loop forever, or until you get sick of waiting and type Ctrl-C to halt it. You need to advance k inside the while loop: while k <= 3: process C[i][j] ... k = k + 1 Remember that the body of the while loop is defined by the *indented* block beneath it. You do have a k = k+1 beneath the while loop, but it isn't indented enough, so it counts as *outside* the while block. I haven't studied it in detail, but you can try fixing that and see if it works. > #def summation > def SUMMATION(x,y): >s=0 >k=0 >j=0 >while (k<5): >sumx=sumx + x[k][2] >k=k+1 >while (j<7): >sumy=sumy + y[2][j] >j=j+1 >s=sumx + sumy This can't work, because sumx and sumy don't have a value to start with. You need to initialize them (perhaps zero?) first. Actually, I don't think you need them at all
Re: [Tutor] I need help with my homework. No, really....
Part 3... On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > following: > > 1. Load array x column-wise and array y row-wise > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #Lab #5 > #COSC 1336-31493 > #SUM 2015 NRG > #Tu/Th 1:15-4:25pm > > def main(): > #matrix initialization > x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] > y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] > > z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] > #file declaration > infile = open('lab5x.dat','r') > infile = open('lab5y.dat','r') > outfile = open('lab5.out', 'w') > #variables > sumx = 0 > sumy = 0 > small = 0 > A = 0 > B = 0 > C = 0 > > #call functions > LOADX(infile, A) > LOADY(infile, B) > COMPUTEZ(A, B, C) > SUMMATION(A, B) > SMALLEST(A) > OUTDATA(file, A, B, C) > #close files > infile.close() > infile.close() > outfile.close() > dummy = input('Press any key to continue.') > #def smallest > def SMALLEST (B): > k=0 > s=B[1][k] > k=k+1 > while (k<7): > if(s> B[1][k]): > s=B[1][k] > k=k+1 I don't think that works at all. There doesn't seem to be any attempt to check for the smallest value. Python has a function, min(), which can take a list of values and returns the smallest of them. So we can do the following: def SMALLEST(B): # Return the smallest value in row 1 of matrix B. get row one pass it to function min() return the result Obviously that's not actual Python code! Now, remember, your matrix looks like this: [ [a, b, c, d], # row 0 [e, f, g, h], # row 1 etc. So getting a row is easy. (Getting a column is trickier.) the_row = B[1] result = min(the_row) return result will put row 1 into variable the_row, then pass it to min(), and finally return it. > def OUTDATA(outfile, x, y, z,SMALLEST,SUMMATION): > i=0 > j=0 > k=0 > while (k<3): >print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4]) >k=k+1 This should be printing the x matrix, but you're using variable A instead, which as far as I understand it, won't exist. I think the easiest fix for this problem is to change the name in the function declaration: def OUTDATA(outfile, x, y, z, SMALLEST, SUMMATION): becomes: def OUTDATA(outfile, A, B, C, SMALLEST, SUMMATION): Note carefully that you have a clash between the names of the *function* SMALLEST and the argument SMALLEST. Python won't be confused, but you may be! I recommend that you change the name in the function declaration. > file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])] Three problems with this one line: (1) The indentation is lost. Maybe that's just an email thing. (2) The variable should be called outfile, not file. (3) You're writing the numbers mashed up together: "12345678" instead of "12,34,56". Here's a little trick: you can join a list of strings with commas like this: list_of_strings = ['12', '34', '56'] print( ','.join(list_of_strings) ) (except you won't use print, you will write it to a file). So first you make a list of numbers making up the row: row = A[k][:] Convert each item from an int to a str: row = [str(n) for n in row] Join with commas: thestring = ','.join(row) and finally write it to the file: # don't forget the newline at the end of each line outfile.write(thestring + '\n') > while (j<7): > print(B[j][0],B[j][1],B[j][2]) > j=j+1 > file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])] > while (i<7): > print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4]) > file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])] Again, I believe these will run all the numbers together. > print ('Summation= ',SUMMATION) > file.write('Summation= ', SUMMATION) The print() line is okay, because Python will happily print ints as w
Re: [Tutor] String Attribute
On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Root and power
On Tue, Jul 28, 2015 at 08:29:00PM -0700, Job Hernandez wrote: [...] > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. Let's suppose the user entered 36. Then the possible answers are: 36**1 = 36 6**2 = 36 and I think that's about it. We know that pwr=0 won't give any solutions unless the number itself is 1: 1**0 = 1 2**0 = 1 3**0 = 1 4**0 = 1 etc. So if the user enters 1, you can just print root=1 and pwr=0 and you are done. (For that matter, you could print any value for root!) Otherwise, for any pwr other than 1, we want to find some root such that: root**pwr = the number the user entered How might we do this for, say, pwr=2, and the number 25? There's no built in function for this, instead you need to do a loop, testing each number in turn: 1**2 = 1, too small 2**2 = 4, too small 3**2 = 9, too small 4**2 = 16, too small 5**2 = 25, equals the user's number so this tells us that 25 is a perfect square, and we can now print root=5, pwr=2. How about pwr=2, number = 27? 1**2 = 1, too small 2**2 = 4, too small 3**2 = 9, too small 4**2 = 16, too small 5**2 = 25, too small 6**2 = 36, too big So this tells us that 27 is NOT a perfect square. Let's check to see if it's a perfect cube: 1**3 = 1, too small 2**3 = 8, too small 3**3 = 27, equals the user's number so 27 is a perfect cube, and we can print root=3, pwr=3. Obviously we don't actually need to check root=1, since 1 to the power of anything is always 1. Let's try (say) 59: 2**2 = 4, too small 3**2 = 9, too small ... 7**2 = 49, too small 8**2 = 64, too big -- pwr cannot be 2 2**3 = 8, too small 3**3 = 27, too small 4**3 = 64, too big -- pwr cannot be 3 2**4 = 16, too small 3**4 = 81, too big -- pwr cannot be 4 2**5 = 32, too small 3**5 = 243, too big -- pwr cannot be 5 2**6 = 64, too big -- pwr cannot be 6 At this point you have a choice: print "No such root and pwr" print "root=59, pwr=1" but I guess the second one is probably going against the spirit of the question. Or maybe not? Hard to say. Obviously you shouldn't write out all the tests by hand: # No, don't do this! if 2**2 == number: print("root=2, pwr=2") elif 3**2 == number: print("root=3, pwr=2") elif 4**2 == number: print("you've got to be kidding, I quit!") Instead you will use for-loops and range, and break to exit out of the loop early. for pwr in range(2, 7): for root in range(2, num): ... Is that enough of a hint, or do you need more assistence? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range
On Tue, Jul 28, 2015 at 11:03 AM, Oscar Benjamin wrote: > On Mon, 27 Jul 2015 at 20:53 Colin Ross wrote: > >> *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following >> conditions: >> - Green for 0 < x < 4 >> - Red for 4 < x < 12 >> >> *Code: * >> >> *Note: Code currently only attempting to shade green for 0 < x < 4 * >> >> import numpy as np >> import pylab >> from pylab import * >> import matplotlib.pyplot as plt >> import csv >> >> >> # Load data from .txt file >> >> with open('current_mirror_output_swing.csv', 'rb') as f: >>reader = csv.reader(f) >>your_list = list(reader) >> >> data = np.asarray(your_list) >> >> I_ref = np.asarray(data[1:,0]) >> I_1 = data[1:,1] >> I_2 = data[1:,2] >> I_3 = data[1:,3] >> >> # Create an array of x values to fill b/w curves with a certain color. >> >> > Here you specify the X values for the fill shape as going from 0 to 4: > > >> X1 = np.linspace(0.,4.,len(I_3)) >> >> I_ref = I_ref.astype(float)*1000. >> I_1 = I_1.astype(float)*1000. >> I_2 = I_2.astype(float)*1000. >> I_3 = I_3.astype(float)*1000. >> >> >> # Plotting commands. >> >> > Here you specify the X values for the line plots as being whatever I_ref > is (I don't know what it is since I can't see your csv file): > > >> plot(I_ref, I_2, 'r-') >> plot(I_ref, I_3, 'b-') >> title('Current Mirror Output Swing') >> xlabel('$I_{ref}$ (mA)') >> ylabel('I (mA)') >> >> > Try changing this line: > > >> plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') >> > > to: > > plt.fill_between(I_ref, I_2, I_3, color = 'g', alpha = '0.5') > > and then the filled area should have the same X range as the lines. > > -- > Oscar > Thanks! This works now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Arrow of constant size on log plot
I am attempting to draw an arrow that begins at a specified point on a logarithmic plot and then extends a certain distance in the -Y direction. This part is fine, however I would like to draw multiple arrows with the same size corresponding to different points on a plot. If I specify a certain value (as shown in the example below), the arrow size will change depending on where the point is on the plot, since the X and Y axis are logarithmic. - import numpy as np import pylab as pl x_1 = np.arange(10) y_1 = np.arange(10) x_1_avg = np.sum(x_1)/len(x_1) y_1_avg = np.sum(y_1)/len(y_1) x_2 = 3. y_2 = 3. pl.plot(x_1,y_1,'k') pl.arrow(x_1_avg,y_1_avg , 0.0, -0.5, fc='b', ec='b', head_width=0.1, head_length=0.1) pl.arrow(x_2,y_2, 0.0, -0.5, fc='r', ec='r', head_width=0.1, head_length=0.1 ) pl.yscale('log') pl.xscale('log') pl.show() - Any advice is appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Basic question about docstrings
Hi If I have a script called main.py and document a function in it: def get_value(x): """ Some text ... :param x: Some value :returns: Something useful """ What is the most basic way of showing those docstrings at the Python prompt? For getting started with documentation, is Sphinx a good way to go, or would you recommend something simpler? Best regards David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] mbox-short
Hi Everyone, How do I file in the empty list at 0 on line # 3 to produce the desired output: The first goal of the program is to produce an output from the date list file as following: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu c...@iupui.edu c...@iupui.edu And secondly, print out a count at the end Raw Python code is available at http://tinyurl.com/p4k8qa4 The data input file is available at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mbox-short
> How do I file in the empty list at 0 on line # 3 to produce the desired output: What trouble are you having? Please try to describe where you are getting stuck. What have you tried? Is there anything confusing? Also note that a few of your peers have asked the exact same homework assignment on this list. We are not a homework answering service: rather, we are a group of volunteers to help beginners learn to program. The distinction is that we actually care about *why* you're having difficulty. The homework assignment itself is not our focus: what matters to us is how to help you learn how to solve these kinds of problems. So, can you say more why you're getting stuck? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
On 7/29/2015 8:45 AM, David Aldrich wrote: Hi If I have a script called main.py and document a function in it: def get_value(x): """ Some text ... :param x: Some value :returns: Something useful """ What is the most basic way of showing those docstrings at the Python prompt? Are you asking about help? as in: >>> help(get_value) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
On Jul 29, 2015 12:45 PM, "David Aldrich" wrote: > > Hi > > If I have a script called main.py and document a function in it: > > def get_value(x): > """ > Some text ... > :param x: Some value > :returns: Something useful > """ > > What is the most basic way of showing those docstrings at the Python prompt? Try: help(get_value) At the Python prompt. This uses the built in help facility: https://docs.python.org/2/library/functions.html#help ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mbox-short
On 29/07/2015 19:38, ltc.hots...@gmail.com wrote: I have no intention of answering a question that contains massives of whitespace and no code. Please post your code inline. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mbox-short
On 29/07/15 19:38, ltc.hots...@gmail.com wrote: How do I file in the empty list at 0 on line # 3 to produce the desired output: I have no idea what you mean? What does "file in the empty list" mean? What are you trying to do? The first goal of the program is to produce an output from the date list file as following: Should that be "data list" rather than date list? stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu And secondly, print out a count at the end The count bit is easily done once you have the other bit working. But its not clear what you re asking us to do. Also please post the code in the mail rather than pastebin. Pastebin is fine if its a big program (>100 lines say) but for small programs just paste the code into the email. Its much easier to read and comment on that way. Raw Python code is available at http://tinyurl.com/p4k8qa4 The data input file is available at http://www.pythonlearn.com/code/mbox-short.txt -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Mailbox
Hi Everyone: I have a second and unrelated question: I tried to work backward to see if there is a logic error associated with a variable is being skipped over: #top of code, initialize variable output_list = ["default"] #rest of code If you get at the end print output_list ['default'] Raw Data File: count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startswith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" Sample data file at http://www.pythonlearn.com/code/mbox-short.txt Desired Output: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu Thanks, Hal Sent from Surface From: Phu Sam Sent: Wednesday, July 29, 2015 1:06 PM To: Ltc Hotspot ___ Baypiggies mailing list baypigg...@python.org To change your subscription options or unsubscribe: https://mail.python.org/mailman/listinfo/baypiggies ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mailbox
On 29Jul2015 21:55, ltc.hots...@gmail.com wrote: I have a second and unrelated question: I tried to work backward to see if there is a logic error associated with a variable is being skipped over: #top of code, initialize variable output_list = ["default"] Curious: why not just make this an empty list: [] [...snip...] count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startswith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" [...snip...] My first observation would be that "count = count + 1" is not inside the loop. That means that it fires just once, _after_ the loop. So it will always be 1. Also, what is the purpose of this line: line = line.split() Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor