Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread Noufal Ibrahim
Kent Johnson wrote: [..] Why not just return the value from the function and pass it up the call chain? If a call fails return None. Something like this: That's what I ended up doing but the first thing occurred to me and I was just wondering if there's any production code that relies on the

Re: [Tutor] reading variables in a data set?

2009-07-05 Thread Kent Johnson
On Sat, Jul 4, 2009 at 12:09 PM, Steven Buck wrote: > I've used a module (StataTools) from (http://presbrey.mit.edu/PyDTA ) to get > a Stata ".dta" file into Python. In Stata the data set is an NXK matrix > where N is the number of observations (households) and K is the number of > variables. > I

Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread bob gailer
Noufal Ibrahim wrote: Kent Johnson wrote: [..] Why not just return the value from the function and pass it up the call chain? If a call fails return None. Something like this: That's what I ended up doing but the first thing occurred to me and I was just wondering if there's any production co

[Tutor] Poor style to use list as "array"?

2009-07-05 Thread Angus Rodgers
The problem this time is, given an amount of US currency in cents (and less than a dollar), to print out a description of the coins needed to represent it, using the smallest number of coins. E.g.: How many cents this time? 59 2 quarters, 1 nickel and 4 pennies. How many cents this time? 55 2 qu

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Noufal Ibrahim
Angus Rodgers wrote: [..] This feels like a bit of a cheat, as if I am trying to program in Python as if it were some other more familiar language. Should I have coded this differently? I can't offer anything concrete but here are some things that occur to me at a glance. 0. You can try to

Re: [Tutor] Is my style OK in this elementary student exercise?

2009-07-05 Thread Tim Peters
[Angus Rogers, suffering eval-angst] > ... > On the other hand, so long as I AM only executing the function > myself, I am no more at risk than I already am every single time > I type a command into a Python interpreter, of any description. > (A somewhat Existentialist thought, perhaps!  Virtual su

Re: [Tutor] Stack unwind using exceptions.

2009-07-05 Thread Eike Welk
On Sunday 05 July 2009, Noufal Ibrahim wrote: > Kent Johnson wrote: > [..] > > > Why not just return the value from the function and pass it up > > the call chain? If a call fails return None. Something like this: > > That's what I ended up doing but the first thing occurred to me and > I was just

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Kent Johnson
On Sun, Jul 5, 2009 at 2:48 PM, Angus Rodgers wrote: > for i in range(LEN - 1): >    (count[i], amnt) = divmod(amnt, value[i]) How about this: counts = [] for val in value: count, amnt = divmod(amnt, val) counts.append(count) > This feels like a bit of a cheat, as if I am trying to program i

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Alan Gauld
"Angus Rodgers" wrote as keys? Is it possible to guarantee a sequence in which the keys of a dictionary are iterated through? Indirectly yes: for key in sorted( dct ): print key I would definitely tend to go with a dictionary for the denominations/values in this case (Actually I'd

[Tutor] append question

2009-07-05 Thread Steven Buck
Hi Python Tutors: I have a data structure that looks like: >>> test=[[1,2,3],[4,5,6],[7,8,9]] I want to define a new variable that captures the second element of each sublist from above: >>> testvar2 = [] Next I try to capture the aforementioned elements: >>> for i in len(test): t

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Angus Rodgers
On Sun, 5 Jul 2009 18:49:32 -0400, Kent Johnson wrote: >On Sun, Jul 5, 2009 at 2:48 PM, Angus Rodgers wrote: > >> for i in range(LEN - 1): >>    (count[i], amnt) = divmod(amnt, value[i]) Incidentally, I forgot to quote the next line: count[-1] = m Of course, this is much better incorporated int

Re: [Tutor] append question

2009-07-05 Thread Luke Paireepinart
Read your error message... It highlighted the first line of your for loop ansd said ints aren't iterable. len(list) returns an integer. You want a list of items... for i in range(len(list)): On 7/5/09, Steven Buck wrote: > Hi Python Tutors: > > I have a data structure that looks like: > test

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Angus Rodgers
On Mon, 06 Jul 2009 01:02:10 +0100, I hastily wrote: >Incidentally, I forgot to quote the next line: > >count[-1] = m That was copied-and-pasted from an older version of the program, with less descriptive identifiers. 'm' should be 'amnt'. >Of course, this is much better incorporated into the l

Re: [Tutor] append question

2009-07-05 Thread Rich Lovely
2009/7/5 Steven Buck : for i in len(test): >     testvar2.append(test[i][2]) > > I want testvar2 = [2,5,8] but instead I get the following error message: > > Traceback (most recent call last): >   File "", line 1, in >     for i in len(test): > TypeError: 'int' object is not iterable

Re: [Tutor] append question

2009-07-05 Thread Robert Berman
In [1]: test=[[1,2,3],[4,5,6],[7,8,9]] In [3]: testvar2 = [] In [16]: for i in range(len(test)): : testvar2.append(test[i][1]) : : In [17]: testvar2 Out[17]: [2, 5, 8] Robert On Sun, 2009-07-05 at 15:57 -0700, Steven Buck wrote: > Hi Python Tutors: > > I ha

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread Rich Lovely
>                if name in plural: >                    name = plural[name] >                else: >                    name += 's' This could be written more cleanly (although arguably not as readably) as name = plural.get(name, name + "s") d.get(key, default) returns the value from d mapped to

Re: [Tutor] Poor style to use list as "array"?

2009-07-05 Thread bob gailer
Angus Rodgers wrote: The problem this time is, given an amount of US currency in cents (and less than a dollar), to print out a description of the coins needed to represent it, using the smallest number of coins. E.g.: How many cents this time? 59 2 quarters, 1 nickel and 4 pennies. How many c

Re: [Tutor] append question

2009-07-05 Thread Rich Lovely
2009/7/6 Steven Buck : > Thanks for the previous responses.  This isn't homework--I'm beyond > coursework, although I am a newbie to Python (and I've never had to do much > real programming since I've just used Stata for econometric analysis).  I'm > testing Python as a more powerful alternative to

[Tutor] Python Tutorials: How to create useful programs after learning the syntax?

2009-07-05 Thread Luis Galvan
Hello all, this is my first time using a mailing list, so I'm not sure if I'm doing this right! Anyway, I have a wee bit of a problem. I've recently completed watching a Youtube video series on Python 2.6 by thenewboston which helped me a TON with learning Python's syntax and how to use some of t