Re: [Tutor] Help on Python Looping Please

2011-02-24 Thread Walter Prins
On 24 February 2011 14:52, pyhx0r pyh...@gmail.com wrote:

 *Why do in my code, it loops to all values and not in Mark Pilgrim’s code?
 *


Because in Mark's code the loop is terminated by the return statement
(contained in the utility function approximate_size().)  In your code you've
removed the entire function including the return statement, consequently the
loop runs to completion.

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Python Looping Please

2011-02-24 Thread Dave Angel

On 01/-10/-28163 02:59 PM, pyhx0r wrote:

Dear All,


snip
 multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
 for suffix in SUFFIXES[multiple]:
 size /= multiple
 if size  multiple:
 return '{0:.1f} {1}'.format(size, suffix)



 snip

I’ve shorted the code be:



SUFFIXES = {1000: ['KB','MB','GB'],


 1024: ['KiB','MiB','GiB']}



multiple = 1000



size = 2300
for suffix in SUFFIXES[multiple]:

 size /= multiple
 if size  multiple:
 '{0:.1f} {1}'.format(size, suffix)

snip


*Why do in my code, it loops to all values and not in Mark Pilgrim’s code?*



(Is there a reason you double-spaced all that code?  It makes it very 
hard to read, and quite difficult to quote, since I had to delete every 
other line.)


You wrote your code inline, and not as a function.  And you omitted the 
return statement.  So the loop won't return, it'll run to completion.


Another way to exit a loop early is to use the break statement.


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Python Looping Please

2011-02-24 Thread pyhx0r
Dear All,

Thank you for your advise, it's helpful for me.

NB: To DaveA, It was copy-paste from my notepad so the indentation went
wrong :(


+---+--+---+
| py | h | x0r |
+---+--+---+
■ 1 3 0 E2 C 9 ■
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Python Looping Please

2011-02-24 Thread Walter Prins
On 24 February 2011 16:22, Dave Angel da...@ieee.org wrote:


 (Is there a reason you double-spaced all that code?  It makes it very hard
 to read, and quite difficult to quote, since I had to delete every other
 line.)


For what it's worth the code came out perfectly fine on my email reader
(GMail). (No double spacing, courier formatted, all in all pretty easy to
read. What email client are you using?)

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Python Looping Please

2011-02-24 Thread Dave Angel

On 02/24/2011 11:55 AM, Walter Prins wrote:

On 24 February 2011 16:22, Dave Angelda...@ieee.org  wrote:



(Is there a reason you double-spaced all that code?  It makes it very hard
to read, and quite difficult to quote, since I had to delete every other
line.)



For what it's worth the code came out perfectly fine on my email reader
(GMail). (No double spacing, courier formatted, all in all pretty easy to
read. What email client are you using?)

Walter



I use Thunderbird 3.1.7 on Linux 10.04.  In text mode, naturally, but I 
get the same result in html mode.


The prose was properly spaced, and everyone else's messagesare properly 
spaced.  But the code in that particular message was double-spaced.



DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor