Re: [Tutor] Elementtree and pretty printing in Python 2.7
Jerry Hill, 22.08.2010 16:51: Neither, as far as I know. The XML you get is perfectly valid XML. It's clearly well-formed, but I can't see it being valid without some kind of schema to validate it against. Note that the OP has already written a follow-up but forgot to reply to the original mail. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuous running of a method
On 08/23/2010 01:10 AM, James Mills wrote: On Mon, Aug 23, 2010 at 3:00 PM, Greg Bair wrote: I have a method (I'll call it foo) that will either return None or an object depending on a random value generated. What I want to happen is that if I call foo(), i.e, f = foo() and it returns None, to re-call it until it returns something else. I would think this would be done with a while loop, but can't seem to get the details right. Any help would be greatly appreciated. Quite simple really, and yes you are right. Use a while loop: from random import seed, random from time import time seed(time()) def foo(): ... if 0.5< random()< 0.6: ... return True ... x = foo() while x is None: ... x = foo() ... x True cheers James I don't know why I didn't think of that. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuous running of a method
On Mon, Aug 23, 2010 at 3:00 PM, Greg Bair wrote: > I have a method (I'll call it foo) that will either return None or an object > depending on a random value generated. What I want to happen is that if I > call foo(), i.e, f = foo() and it returns None, to re-call it until it > returns something else. I would think this would be done with a while loop, > but can't seem to get the details right. > > Any help would be greatly appreciated. Quite simple really, and yes you are right. Use a while loop: >>> from random import seed, random >>> from time import time >>> seed(time()) >>> def foo(): ... if 0.5 < random() < 0.6: ... return True ... >>> x = foo() >>> while x is None: ... x = foo() ... >>> x True >>> cheers James -- -- James Mills -- -- "Problems are solved by method" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] continuous running of a method
I have a method (I'll call it foo) that will either return None or an object depending on a random value generated. What I want to happen is that if I call foo(), i.e, f = foo() and it returns None, to re-call it until it returns something else. I would think this would be done with a while loop, but can't seem to get the details right. Any help would be greatly appreciated. Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a prime number program using upper bound of square root of n
On Mon, 23 Aug 2010 07:48:03 am Denis Gomes wrote: > Nick, > >If you are using python 2.X, xrange(2,n) is just like range(2,n), > the xrange function is faster. In python 3.X, they got rid of the > slower range function and renamed xrange to range. A slight over-simplification, but broadly correct. > The x in [x for x > in xrange...] will just go from 2 to the number n, but not including > n. I see people using list comprehensions like this: [x for x in xrange(20)] # for example quite frequently. I've even done it myself. But it's silly and wasteful. That is exactly the same, only slower and more complicated, as: list(xrange(20)) or range(20) Any time you write [x for x in SOMETHING] just drop the list comprehension and use SOMETHING on it's own. (You might need to call list(SOMETHING), but often you don't.) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a prime number program using upper bound of square root of n
On Mon, 23 Aug 2010 04:55:15 am Nick wrote: > Is there a way I can set my email program to insert the ">" symbol to > mark what I'm replying to without manually doing it? Depends on your mail client. What are you using? Outlook? > I get the picture now, that was a great example. I wrote some code > last night before going to bed that did what you were saying checking > if it was divisible by 2, 3, 5, or 7. I'm going to paste that. And > so in my range function in this code could the upper bound be n*.5 > instead of n+1 ? I think you mean n**0.5, not half n. > primes = [2, 3, 5, 7] > def p(n): > for i in range(3, n+1, 2): > if i % 3 == 0 or i % 5 == 0 or i % 7 == 0: > pass > else: > primes.append(i) Some criticisms... (1) The name "p" for the function doesn't give much hint as to what it does. What does it mean? "print"? "probability"? "prime numbers less than n"? "the first n primes"? "is n a prime number"? "prime factorization of n"? You know the answer *now*, but will you remember in six months? (2) Using global data is usually a bad thing. You should be able to call the function twice without breaking things, but in this case, if you call it twice, the primes list will have numbers added twice. My suggestion is to move the list of primes into the function, rather than a global variable, and start with a function like this: def primes(n): """Return a list of primes less than or equal to n.""" if n < 2: return [] # No primes less than two, so we're done. results = [2] for i in xrange(3, n+1): if is_prime(i): results.append(i) return results Now all you need is to write a function is_prime(n) that decides whether or not n is prime. def is_prime(n): """Returns True if n is prime, otherwise False.""" if n == 2: return True elif (n % 2 == 0) or (n < 2): return False # If we get here, we know that n is an odd number. is_prime() is not complete. You have to write the rest. (Note: this approach isn't the most efficient way of doing it, but it is probably the simplest. Once you have this working, you can think about ways to make it more efficient.) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a prime number program using upper bound of square root of n
Nick, If you are using python 2.X, xrange(2,n) is just like range(2,n), the xrange function is faster. In python 3.X, they got rid of the slower range function and renamed xrange to range. The x in [x for x in xrange...] will just go from 2 to the number n, but not including n. This brings up a possible issue. You may or may not want to include that number n if n itself is a prime number, therefore you should add 1 to n. The code should then go as follows. def p(n): return [2,3,5,7]+[x for x in xrange(2,n+1) if x%2!=0 and x%3!=0 and x%5!=0 and x%7!=0] Denis On Sun, Aug 22, 2010 at 5:10 PM, Nick wrote: > I will play with this and see what happens. One question though, [x for > x in xrange(2,n) .. what is xrange or is this an error? > -- > *From:* Denis Gomes [denisg...@gmail.com] > *Sent:* Sunday, August 22, 2010 4:07 PM > *To:* Nick > *Subject:* Re: [Tutor] Writing a prime number program using upper bound of > square root of n > > Hey Nick, > >You can also try using list comprehensions to do the same thing. You > could do something as follows: > > def p(n): > return [2,3,5,7]+[x for x in xrange(2,n) if x%2!=0 and x%3!=0 and > x%5!=0 and x%7!=0] > >This is cleaner and probably faster. More Pythonic. > > Denis > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a prime number program using upper bound of square root of n
"Nick" wrote Is there a way I can set my email program to insert the ">" symbol to mark what I'm replying to without manually doing it? Give us a clue. What is your email program? You can do it in most programs but the method will be different in each! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
Please try and reply to the list instead of just me. raw_input did not the trick. fruit.count is the next exercise. Oke, I deleted the initialazion and change teller into letter. Roelof Should be alright now.. Hmmm Can you paste your exact code AND the error you're getting? As I understand you never get the prompts, right? Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
On 08/22/2010 09:35 PM, Roelof Wobben wrote: Hello, I made this programm : def count_letters(n,a): count = 0 for char in n: if char == a: count += 1 return count fruit="" letter="" fruit= input("Enter a sort of fruit: ") teller = input("Enter the character which must be counted: ") x=count_letters (fruit,letter) print "De letter", letter , "komt", x , "maal voor in het woord", fruit The problem is that I can't have the opportuntity for input anything. I use python 2.7 on a Win7 machine with as editor SPE. Roelof Also, you have a typo in your code (teller instead of letter) so fix that too :) A couple more comments: - You don't have to initialize fruit and letter. Doesn't make sense in this context. - Although I can see you made the count_letters() function as an exercise, there is a build in method of string that accomplishes the same effect. Try x=fruit.count(letter) - Give a bit of thought about what would happen if someone enters a whole string instead of a character for letter. How should your program accommodate that? Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
On 8/22/10, Roelof Wobben wrote: > > Hello, > > > > I made this programm : > > > > def count_letters(n,a): > count = 0 > for char in n: > if char == a: > count += 1 > return count > > fruit="" > letter="" > fruit= input("Enter a sort of fruit: ") > teller = input("Enter the character which must be counted: ") > x=count_letters (fruit,letter) > print "De letter", letter , "komt", x , "maal voor in het woord", fruit > > > > The problem is that I can't have the opportuntity for input anything. Try replacing the two lines where you say "input" with "raw_input" and see if that works. > > I use python 2.7 on a Win7 machine with as editor SPE. > > > > Roelof > > > -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a prime number program using upper bound of square root of n
"1 * 120 = 120 2 * 60 = 120 3 * 40 = 120 4 * 30 = 120 5 * 24 = 120 6 * 20 = 120 8 * 15 = 120 10 * 12 = 120 10.9545 * 10.9545 = 120 12 * 10 = 120 <=== already seen this one! > Also I can write a program that > tests whether the number is a factor of 2, 3, 5, 7, but I think > you're trying to point to me that this is cumbersome and not > necessary. Sort of. I'm not suggest that you create lists of multiples of 2, 3, 5, 7 etc, and then cross off non-primes by checking to see if they are in one of those lists. That truly would be cumbersome and slow. But the two problems are similar. For simplicity, let's ignore 2 as a prime, and only think about odd numbers: (1) For each odd number N between 3 and (say) 1000, check if it is a multiple of 3, 5, 7, 9, ... and IF SO, put it in the list "nonprimes". versus: (2) For each odd number N between 3 and (say) 1000, check if it is a multiple of 3, 5, 7, 9, ... and IF NOT, put it in the list "primes". See, they are very nearly the same problem. The tricky bits are: * dealing with 2 is a special case; * you don't want to exclude (say) 3 from being a prime because it is divisible by 3; and * you can stop testing at square-root of N. Steven D'Aprano" Is there a way I can set my email program to insert the ">" symbol to mark what I'm replying to without manually doing it? I get the picture now, that was a great example. I wrote some code last night before going to bed that did what you were saying checking if it was divisible by 2, 3, 5, or 7. I'm going to paste that. And so in my range function in this code could the upper bound be n*.5 instead of n+1 ? is that how I would eliminate the checking of the repeated factors ? I hope I'm not beating a dead horse guys. thankis primes = [2, 3, 5, 7] def p(n): for i in range(3, n+1, 2): if i % 3 == 0 or i % 5 == 0 or i % 7 == 0: pass else: primes.append(i) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
On 08/22/2010 09:35 PM, Roelof Wobben wrote: Hello, I made this programm : def count_letters(n,a): count = 0 for char in n: if char == a: count += 1 return count fruit="" letter="" fruit= input("Enter a sort of fruit: ") teller = input("Enter the character which must be counted: ") x=count_letters (fruit,letter) print "De letter", letter , "komt", x , "maal voor in het woord", fruit The problem is that I can't have the opportuntity for input anything. I use python 2.7 on a Win7 machine with as editor SPE. Roelof You are using Python 2.x, so you should use raw_input() instead of input(). Note that in Python 3.x, raw_input() has been renamed to just input(), with the old 2.x input() gone. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] input problem
Hello, I made this programm : def count_letters(n,a): count = 0 for char in n: if char == a: count += 1 return count fruit="" letter="" fruit= input("Enter a sort of fruit: ") teller = input("Enter the character which must be counted: ") x=count_letters (fruit,letter) print "De letter", letter , "komt", x , "maal voor in het woord", fruit The problem is that I can't have the opportuntity for input anything. I use python 2.7 on a Win7 machine with as editor SPE. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] type() problem
Hello, Very wierd. >From a python shell it works. If a type it as module in SPE , it doesn't work. Roelof > Date: Sun, 22 Aug 2010 19:47:09 +0200 > From: knack...@googlemail.com > To: tutor@python.org > Subject: Re: [Tutor] type() problem > > > > fruit="ramadana" > > print "fruit heeft als type", type(fruit) > > y=len(fruit) > > print y > > > > z=fruit[:3] > > print z > > > > These lines put in a module and executed print (using Python 2.7): > > fruit heeft als type > 8 > ram > > Strange that it doesn't work for you. > > I can only advice you to check the file again or run the code from the > Python shell. > > HTH, > > Jan > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] type() problem
fruit="ramadana" print "fruit heeft als type", type(fruit) y=len(fruit) print y z=fruit[:3] print z These lines put in a module and executed print (using Python 2.7): fruit heeft als type 8 ram Strange that it doesn't work for you. I can only advice you to check the file again or run the code from the Python shell. HTH, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Elementtree and pretty printing in Python 2.7
Sorry wrong import! from xml.minidom import parseString from xml.etree import ElementTree Karim On 08/22/2010 05:24 PM, Karim wrote: Hello Jerry, Tricky solution using minidom (standard) Not tested: import ElementTree import minidom def prettyPrint(element): txt = ElementTree.tostring(element) print minidom.parseString(txt).toprettyxml() Regards Karim On 08/22/2010 04:51 PM, Jerry Hill wrote: On Fri, Aug 20, 2010 at 3:49 AM, Knacktus wrote: Hi guys, I'm using Python 2.7 and the ElementTree standard-lib to write some xml. My output xml has no line breaks! So, it looks like that: instead of something like this: I'm aware of lxml which seems to have a pretty print option, but I would prefer to use the standard-lib ElementTree which seems not to have a feature like this. Do I miss something using the ElementTree-lib or is it bug? Neither, as far as I know. The XML you get is perfectly valid XML. If you want to pretty print it, there's a recipe here: http://effbot.org/zone/element-lib.htm#prettyprint, but I don't think it's been included in the standard library yet. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Elementtree and pretty printing in Python 2.7
On Fri, Aug 20, 2010 at 3:49 AM, Knacktus wrote: > Hi guys, > > I'm using Python 2.7 and the ElementTree standard-lib to write some xml. > > My output xml has no line breaks! So, it looks like that: > > > > instead of something like this: > > > > > > I'm aware of lxml which seems to have a pretty print option, but I would > prefer to use the standard-lib ElementTree which seems not to have a feature > like this. > > Do I miss something using the ElementTree-lib or is it bug? Neither, as far as I know. The XML you get is perfectly valid XML. If you want to pretty print it, there's a recipe here: http://effbot.org/zone/element-lib.htm#prettyprint, but I don't think it's been included in the standard library yet. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] type() problem
Hello, This is my exercise: Write Python code to make each of the following doctests pass: """ >>> type(fruit) >>> len(fruit) 8 >>> fruit[:3] 'ram' """ I have made this : >>> type(fruit) >>> len(fruit) 8 >>> fruit[:3] 'ram' """ fruit="ramadana" print "fruit heeft als type", type(fruit) y=len(fruit) print yz=fruit[:3] print zEverything works fine except the type () does not give any input.WWhat am I doing wrong Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 78, Issue 99 -- Prime numbers
"Nick" wrote I think I'm doing everything correctly now, but please tell me what I'm not-- if such is the case. One last thing - change the subject line to reflect the actual subject. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor