Re: [Tutor] while loop
On Mar 30, 2014, at 4:29 AM, Dave Angel wrote: > > > You're getting closer. Remember that the assignment shows your > function being called with 10, not zero. So you should have a > separate local variable, probably called I, which starts at > zero, and gets incremented each time. The exercise just asks to print (s), (n) times using iteration. The exersise is in a doctest which I didn’t really understand at first. So, I guess while I was doing it “correctly” it wasn’t what the exercise is asking for. This I guess is what the doctest is looking for. """Print the string `s`, `n` times. Parameters -- s -- A string n -- an integer, the number of times to print `s' Examples >>> print_n("hello", 3) hello hello hello >>> print_n("bye", 0) >>> print_n("a", 6) a a a a a a """ > > The test in the while should be comparing them. > > Note that the number of times is specified in top level code, and > implemented in the function. You should not have a literal 10 in > the function. Without out a break or placing that 10 in there I can’t think of a way to have the while loop stop once it reaches (n). Any hints? SCott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
On Mar 30, 2014, at 4:29 AM, Dave Angel wrote: > > You're getting closer. Remember that the assignment shows your > function being called with 10, not zero. So you should have a > separate local variable, probably called I, which starts at > zero, and gets incremented each time. > > The test in the while should be comparing them. > So, this is what I have now and it ‘works’ but, instead of printing (s) on seperate lines they’re all on the same line? def print_n(s,n): while n < 10: print s * n break assert isinstance(s, str) assert isinstance(n, int) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
On 31/03/2014 03:13, Scott Dunning wrote: On Mar 30, 2014, at 4:29 AM, Dave Angel wrote: You're getting closer. Remember that the assignment shows your function being called with 10, not zero. So you should have a separate local variable, probably called I, which starts at zero, and gets incremented each time. The test in the while should be comparing them. So, this is what I have now and it ‘works’ but, instead of printing (s) on seperate lines they’re all on the same line? def print_n(s,n): while n < 10: print s * n break assert isinstance(s, str) assert isinstance(n, int) They say that the truth hurts, so if that's the best you can come up with, I suggest you give up programming :( -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
On 31/03/14 02:37, Scott Dunning wrote: You're getting closer. Remember that the assignment shows your function being called with 10, not zero. So you should have a separate local variable, probably called I, which starts at zero, and gets incremented each time. Without out a break or placing that 10 in there I can’t think > of a way to have the while loop stop once it reaches (n). Dave has explained in his first paragraph(above) how to do it. n is a parameter in your function so the value is passed in by the caller. You should not be using a literal 10 you should be using n, since that's the required number of repeats. Then you need to create a new local variable in your function and let that variable count up until it equals whatever n is. That's where the iteration comes in. And as you count up, towards n, print s. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] while loop
On 31/03/14 03:13, Scott Dunning wrote: separate local variable, probably called I, which starts at zero, and gets incremented each time. The test in the while should be comparing them. So, this is what I have now and it ‘works’ It doesn't work because they are all on the same line. But also because it does NOT use iteration. Your while loop is completely redundant. You could remove the while and break lines and it would do exactly the same. def print_n(s,n): while n < 10: print s * n break You're other attempt where you increment n is much closer to what is being asked for. The only difference is you need to modify the while test to not use a hard coded 10 but use the parameter instead. Then use a separate value to do the counting. Incidentally, your assignment does not appear to require a while loop, just iteration? If thats the case you could use a for loop instead and it would actually be more suitable. Have you covered for loops yet? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] while loop
Scott Dunning Wrote in message: > > On Mar 30, 2014, at 4:29 AM, Dave Angel wrote: >> >> You're getting closer. Remember that the assignment shows your >> function being called with 10, not zero. So you should have a >> separate local variable, probably called I, which starts at >> zero, and gets incremented each time. >> >> The test in the while should be comparing them. >> > So, this is what I have now and it âworksâ but, instead of printing (s) > on seperate lines theyâre all on the same line? > > def print_n(s,n): > while n < 10: > print s * n > break > assert isinstance(s, str) > assert isinstance(n, int) > > So much for getting closer. Go back to the version I replied to. Do you know how to define and initialize a second local variable? Create one called i, with a value zero. You test expression will not have a literal, but compare the two locals. And the statement that increments will change i, not n. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Storing dictionary value, indexed by key, into a variable
Hey all, I am writing a program to drill the user on Latin demonstrative pronouns and adjectives (DPA). It displays a description, and the user has to enter the DPA that corresponds to the description. DPA vary for gender, number and case, and there are 3 separate DPA. I have these stored in a bunch of dictionaries, with the DPA, gender and number in the dictionary name and the cases as keys. Of course, the values are the DPA themselves. Like so: that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'illīus', 'dat': 'illī', 'acc': 'illum', 'abl': 'illō'} I have a function that randomly selects one of these dictionaries, and another that randomly selects strings corresponding to the keys ('nom', 'gen', etc.). The trouble begins somewhere along here: D = chooseDict() c = chooseCase() print(D, c) guess = '' # code to get the guess # then, answer = D[c] if guess == answer: # Do stuff, change score, continue, etc. This doesn't work, and I get this error: TypeError: string indices must be integers So my question is, why does Python think that D is a string? When I type the actual names (i.e., that_those_Masculine_Singular["nom"]) the answer is returned just fine. I have tried D['c'] and D["c"] also, and got the same error. I searched the web, and I can find no explanation on how to do what I am doing, and I can find nothing that indicates why this doesn't work. I'd really appreciate any help! Thank you, J ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
* Scott Dunning [2014-03-30 18:37]: > Without out a break or placing that 10 in there I can’t think of a way > to have the while loop stop once it reaches (n). Any hints? As discussed already, you can't use fixed values (ie, you don't know that 10 is always going to be there). > def print_n(s, n): > > while n <= 10: > > print s > > n = n + 1 > > So, instead of while n <= 10: Think about: while something <= n: and changing something and retesting. -- David Rock da...@graniteweb.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Storing dictionary value, indexed by key, into a variable
On 2014-03-31 06:38, John Aten wrote: Hey all, I am writing a program to drill the user on Latin demonstrative pronouns and adjectives (DPA). It displays a description, and the user has to enter the DPA that corresponds to the description. DPA vary for gender, number and case, and there are 3 separate DPA. I have these stored in a bunch of dictionaries, with the DPA, gender and number in the dictionary name and the cases as keys. Of course, the values are the DPA themselves. Like so: that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'illīus', 'dat': 'illī', 'acc': 'illum', 'abl': 'illō'} I have a function that randomly selects one of these dictionaries, and another that randomly selects strings corresponding to the keys ('nom', 'gen', etc.). The trouble begins somewhere along here: D = chooseDict() c = chooseCase() print(D, c) guess = '' # code to get the guess # then, answer = D[c] if guess == answer: # Do stuff, change score, continue, etc. This doesn't work, and I get this error: TypeError: string indices must be integers So my question is, why does Python think that D is a string? When I type the actual names (i.e., that_those_Masculine_Singular["nom"]) the answer is returned just fine. I have tried D['c'] and D["c"] also, and got the same error. I searched the web, and I can find no explanation on how to do what I am doing, and I can find nothing that indicates why this doesn't work. I'd really appreciate any help! Thank you, J ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Assuming that it is the answer = D[c] statement is giving you the TypeError, I suggest you add the following print statements just before it: print("'D' is of type %s"%(type(D), ) print("'c' = %s and is of type %s."%(c, type(c), ) You might get some surprises. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Storing dictionary value, indexed by key, into a variable
John Aten wrote: > Hey all, > > I am writing a program to drill the user on Latin demonstrative pronouns > and adjectives (DPA). It displays a description, and the user has to enter > the DPA that corresponds to the description. DPA vary for gender, number > and case, and there are 3 separate DPA. I have these stored in a bunch of > dictionaries, with the DPA, gender and number in the dictionary name and > the cases as keys. Of course, the values are the DPA themselves. Like so: > > that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'illīus', 'dat': > 'illī', 'acc': 'illum', 'abl': 'illō'} > > I have a function that randomly selects one of these dictionaries, and > another that randomly selects strings corresponding to the keys ('nom', > 'gen', etc.). The trouble begins somewhere along here: Unfortunately the problem is in the code you don't show. You might add the line > D = chooseDict() print("D is of type", type(D)) and if that prints D is of type you can be sure that the problem originates in the chooseDict() function. Try to find the error yourself first, looking closely at the function's code, and if you run out of ideas or places where you can put print() calls for debugging purposes come back here. Don't forget to include the code this time. > c = chooseCase() > > print(D, c) > > guess = '' > # code to get the guess > # then, > answer = D[c] > > if guess == answer: > # Do stuff, change score, continue, etc. > > This doesn't work, and I get this error: > > TypeError: string indices must be integers As a general note, error messages are most useful when the are accompanied by the traceback. As it stands I have no idea what line triggers the error; you might have the line ""[""] somewhere in your code. > So my question is, why does Python think that D is a string? When I type > the actual names (i.e., that_those_Masculine_Singular["nom"]) the answer > is returned just fine. I have tried D['c'] and D["c"] also, and got the > same error. I searched the web, and I can find no explanation on how to do > what I am doing, and I can find nothing that indicates why this doesn't > work. I'd really appreciate any help! If the D in the line D = chooseDict() is actually a dict you are most certainly reassigning D = "some string" or you have two different variables named "D" in separate scopes. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Python bingo game.
> Hello, > > Can some one help me with displaying a matrix vertically. > > For example my output matrix is:- > > [1 2 5 7 9] > [25 67 78 23 34] > [33 22 66 88 98] > [32 31 41 56 78] > [21 34 58 99 76] > > And i want my matrix to look like this:- > [1 25 33 32 21] > [2 67 22 31 34] > [5 78 66 41 58] > [7 23 88 56 99] > [9 34 98 78 76] > > Please, help me with the code in eclipse using py-dev as preference. > > Thank you ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Python bingo game.
>> Can some one help me with displaying a matrix vertically. >> >> For example my output matrix is:- >> >> [1 2 5 7 9] >> [25 67 78 23 34] >> [33 22 66 88 98] >> [32 31 41 56 78] >> [21 34 58 99 76] >> >> And i want my matrix to look like this:- >> [1 25 33 32 21] >> [2 67 22 31 34] >> [5 78 66 41 58] >> [7 23 88 56 99] >> [9 34 98 78 76] You have described the "matrix transpose" problem, which is fairly standard homework as an introductory list-processing exercise. Your comment about PyDev and Eclipse is technically irrelevant, so we have to ignore that part of your question. Why should it matter what your program is written in, as long as it's a program? What difficulty are you having? I need to be straightforward so that you understand, without ambiguity: we do not do your homework. We will not violate the honor code of your institution. To do so is anathema to why folks here volunteer to help beginners. We will be happy to help with coding questions or techniques. In some cases, we'll also try to help present problem solving techniques. Have you done a problem that has any similarity to the problem you're tackling now? Have you written functions that work on matrices before? Do you have any smaller test cases? Starting on the 5x5 case is large. Have you considered smaller cases like the 1x1 and 2x2 matrices? Are your matrices always square? What is the representation of your data? What is the type of your input? Can you describe it precisely? What is the type of your output? Can you describe it precisely? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Storing dictionary value, indexed by key, into a variable
> So my question is, why does Python think that D is a string? Assume that Python is telling the truth, at least unless something really unusual is happening. :P Assume D is a string. Your question should really be: why is D a string? Where does "D" get assigned? --- Also note that in your presentation of the bug, the presentation omits a bit of the error message: It presents the error message text, but not the entire "traceback". You don't need to do that, and in fact, you will usually want to be verbose. The context in which the error occurs might be helpful: in particular, you might be misinterpreting the subject or object of the error message. Without seeing context, we can't do a cursory confirmation that the error matches your diagnosis. We want to verify by looking at symptoms. Don't curtail error messages and stack traces, but include them next time. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
On Mar 31, 2014, at 2:01 AM, Alan Gauld wrote: > > Incidentally, your assignment does not appear to require > a while loop, just iteration? If thats the case you could > use a for loop instead and it would actually be more > suitable. Have you covered for loops yet? > No, we haven’t got to for loops yet, that’s why it needs to be done with a while for now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while loop
On Mar 31, 2014, at 1:39 AM, Mark Lawrence wrote: > > They say that the truth hurts, so if that's the best you can come up with, I > suggest you give up programming :( You’re in the TUTOR section. People in here are new to programming. I’ve only been doing this for a couple months and I just learned about while loops two days ago. If my questions are annoying you I suggest you not read them. : ( ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] exercise (while loop)
I’m working on a few exercises and I’m a little stuck on this one. This is what the book has but it just gives me an endless loop. def square_root(a, eps=1e-6): while True: print x y = (x + a/x) / 2 if abs(y-x) < epsilon: break round(square_root(9)) I tweaked it to what I thought was correct but when I test it I get nothing back. def square_root(a, eps=1e-6): x = a/2.0 while True: y = (x + a/x)/2.0 if abs(x - y) < eps: return y x = y round(square_root(9)) The way I tweaked it seems to work, I’m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python. The books way is just print whatever I use for x, so I don’t understand that at all. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mar 31, 2014 6:22 PM, "Scott W Dunning" wrote: > > I’m working on a few exercises and I’m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > > round(square_root(9)) Hi Scott, Ah. I think I see what might be wrong, but let's make sure about this. Can you explain what 'x', 'y' are in this function? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
Also, which book? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
> I tweaked it to what I thought was correct but when I test it I get nothing > back. > > def square_root(a, eps=1e-6): >x = a/2.0 >while True: >y = (x + a/x)/2.0 >if abs(x - y) < eps: >return y >x = y > > round(square_root(9)) > > The way I tweaked it seems to work, I’m getting the correct answer on the > calculator but the interpreter is not returning anything when I check in > python. I didn't want to keep you waiting, so I'll cut to the chase. This line here in your program: round(square_root(9)) computes a value... But it doesn't do anything with that value. Try printing the value. You may also try to see that your program is doing something effective by "unit testing" it. This is often a lot better than just printing values and looking at them, because the test case will say what the _expected_ value is, so it's more informative. For this example, the following is a start at unit testing the above function. Add the following to the bottom of your program's source. ### ## See: http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html import unittest class SquareRootTests(unittest.TestCase): def testSimpleCases(self): self.assertAlmostEqual(square_root(1), 1.0) self.assertAlmostEqual(square_root(4), 2.0) if __name__ == '__main__': unittest.main() ### Here's what it looks like when I run this: ## $ python sqrt.py 4.472135955 . -- Ran 1 test in 0.000s OK ## You can then start adding more and more to tests to gain confidence that the code is doing something reasonable. If we try to put in an intentionally broken test, like: self.assertAlmostEqual(square_root(3), 2.0) in the body of testSimpleCases(), then we'll see the following error when running the program: ## $ python sqrt.py 4.472135955 F == FAIL: testSimpleCases (__main__.SquareRootTests) -- Traceback (most recent call last): File "sq.py", line 20, in testSimpleCases self.assertAlmostEqual(square_root(3), 2.0) AssertionError: 1.7320508075688772 != 2.0 within 7 places -- Ran 1 test in 0.000s FAILED (failures=1) ## And that's what you want to see. If either the test or the code is bad, it'll say something about it. One other thing: you will want to check a particularly insidious case that will cause the program here to behave badly. Consider the zero case: square_root(0). Write the test case. Run it. You'll see something interesting. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Python bingo game.
> > What difficulty are you having? I need to be straightforward so that > you understand, without ambiguity: we do not do your homework. We > will not violate the honor code of your institution. To do so is > anathema to why folks here volunteer to help beginners. > I do want to apologize if the tone of the reply was a bit curt. It's just this: if you provide any information about what you've tried, or what your experience is, you untie our hands and let us help. Otherwise, our hands really are tied and we're limited in what we can say. The more you can say about what you've tried and done, the more freedom you give us to help you. For the question you've provided, if it we're not phrased as a homework question, I'd point directly to the Numpy library, because matrix transpose is already provided in that library: http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html But in your context, I don't think this is what you're asking. So that's another reason why you need to tell us enough sufficient detail that we can give you appropriate advice. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FASTA parsing, biological sequence analysis
On Tue, Mar 25, 2014 at 8:36 AM, Sydney Shall wrote: > I did not know about biopython, but then I am a debutant. > I tried to import biopython and I get the message that the name is unknown. No problem. It is an external library; I hope that you were able to find it! I just want to make sure no one else tries to write yet another FASTA parser badly. It's all too easy to code something quick-and-dirty that almost solves the issue. The devil's in the details. It might be instructive to look at source code. You can look at: https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py and see all the implementation details the Biopython community has had to consider in the real world. These include things like skipping crazy garbage at the beginning of files, https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L40-L45 and providing a stream-like interface by using generators (using the "yield" command): https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L65 But also consider data validation facilities. At least, the Biopython folks have. They provide a way to declare the genomic alphabet to be used: https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L73 https://github.com/biopython/biopython/blob/master/Bio/Alphabet/ where if the input data doesn't match the allowed alphabet, you'll get a good warning about it ahead of time. This is checked in places like: https://github.com/biopython/biopython/blob/master/Bio/Alphabet/__init__.py#L375 https://github.com/biopython/biopython/blob/master/Bio/Seq.py#L336 In short, in the presence of potentially messy data, the developers have thought about these sorts of issues and have programmed for those situations. As the commit history demonstrates: https://github.com/biopython/biopython/commits/master they started work in the last century or so (since at least 1999-12-07), and continue to work on it even now. So taking advantage of their generous and hard work is a good idea. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mon, Mar 31, 2014 at 8:48 PM, Scott W Dunning wrote: > > On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: > Thanks for the info Danny! I’ll try that and I should be able to figure it > out with your help! > > The book I was referring to is greentreepress. The reason I'm asking is I want to double check the example code. Checking... http://greenteapress.com/ ... but Green Tree Press publishes a few Python books. Hmmm. I will guess that you mean: Allen Downey's: "How to Think Like a Computer Scientist". Ah, found it. http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 But please, try to provide details. You tend to suppress helpful details. I would like to avoid guessing next time, so be aware that we don't see what you're thinking. Ok, I see now what you were looking at. But we need to wheel back around to one of your original questions. You said: > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: >break > > round(square_root(9)) Go back and look at that text again: http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 and now see that the book does not present a function in that section. Instead, it's showing exploratory code. There's no function there, all the state is global, and it's not computing a return value. So you shouldn't be too surprised that the code the book is presenting, as a non-functional example, requires some adaptation before it works as a function. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise (while loop)
On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: Thanks for the info Danny! I’ll try that and I should be able to figure it out with your help! The book I was referring to is greentreepress. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor