Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unexplainable python

5 views
Skip to first unread message

dads

unread,
Sep 26, 2009, 10:37:25 PM9/26/09
to
When creating a script that converts digits to words I've come across
some unexplainable python. The script works fine until I use a 5 digit
number and get a 'IndexError: string index out of range'. After
looking into it and adding some print calls, it looks like a variable
changes for no reason. The example beneath is using the digits 34567,
the _5digit function slices 34 off and passes it to the _2digit
function, which works with 2 digit strings but the IndexError is
raised. Please accept my apologies for the explanation, I'm finding it
hard to put into words. Has anyone any idea why it's acting the way it
is?

enter number: 34567
_5digit function used
34 before sent to _2digit
34 slice when at _2digit function
34 before sent to plus_ten function
7 slice when at _2digit function
7 before sent to plus_ten function


from __future__ import print_function
import sys

class number(object):

def __init__(self, number):

#remove any preceding zero's
num = int(number)
self.num = str(num)
self.num = number

self.single =
{'0':'zero','1':'one','2':'two','3':'three','4':'four',

'5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
self.teen = {'11':'eleven','12':'twelve','13':'thirteen',
'14':'fourteen','15':'fifteen','16':'sixteen',

'17':'seventeen','18':'eighteen','19':'nineteen'}
self.plus_ten =
{'10':'ten','20':'twenty','30':'thirty','40':'forty',
'50':'fifty','60':'sixty','70':'seventy',
'80':'eighty','90':'ninety'}
self._translate()

def _translate(self):

fns = [ i for i in number.__dict__ if 'digit' in i ]
fns.sort()
fn_name = fns[len(self.num)-1]
print(fn_name,'function used')
fn = number.__dict__[fn_name]
print(fn(self, self.num))


def _1digit(self, n):

return self.single[n]

def _2digit(self, n):

print(n, 'slice when at _2digit function')
if '0' in self.num:
return self.plus_ten[n]
elif self.num[0] == '1':
return self.teen[n]
else:
print(n,'before sent to plus_ten function')
var = self.plus_ten[n[0]+'0'] + ' ' + self._1digit(n[1])
return var

def _3digit(self, n):

var = self._1digit(n[0]) + ' hundred and ' + self._2digit(n
[1:])
return var

def _4digit(self, n):

var = self._1digit(n[0]) + ' thousand ' + self._3digit(n[1:])
return var


def _5digit(self, n):

print(n[:2],'before sent to _2digit')
var = self._2digit(n[:2]) + ' thousand ' + self._4digit(n[2:])
return var

class control(object):

def __init__(self):
pass

def data_input(self):


while True:
i = raw_input('enter number: ')
if i == 's':
break
#try:
n = number(i)
#except:
# print('not a number')


if __name__ in '__main__':
c = control()
c.data_input()

dads

unread,
Sep 26, 2009, 10:39:22 PM9/26/09
to
Sorry forgot to mention I'm using python 2.6

Chris Rebert

unread,
Sep 26, 2009, 10:50:25 PM9/26/09
to dads, pytho...@python.org
On Sat, Sep 26, 2009 at 7:37 PM, dads <wayne.d...@gmail.com> wrote:
> When creating a script that converts digits to words I've come across
> some unexplainable python. The script works fine until I use a 5 digit
> number and get a 'IndexError: string index out of range'.

Please provide the full error traceback. Help us help you.

<snip>


>    def __init__(self, number):
>
>        #remove any preceding zero's
>        num = int(number)
>        self.num = str(num)
>        self.num = number

I can tell you right now, the first 2 lines of this method have no net effect.

Cheers,
Chris
--
http://blog.rebertia.com

tec

unread,
Sep 26, 2009, 11:33:16 PM9/26/09
to
dads 写道:
> ...

> enter number: 34567
> _5digit function used
> 34 before sent to _2digit
> 34 slice when at _2digit function
> 34 before sent to plus_ten function
> 7 slice when at _2digit function
This is the point. _2digit() only gets 1 digit("7") and needs accessing
the second byte in:

var = self.plus_ten[n[0]+'0'] + ' ' + self._1digit(n[1])

> 7 before sent to plus_ten function
>
> ...


> from __future__ import print_function
> import sys
>
> class number(object):
>

> def _5digit(self, n):
>
> print(n[:2],'before sent to _2digit')
> var = self._2digit(n[:2]) + ' thousand ' + self._4digit(n[2:])

You passed the last 3 digits to _4digit function.

Mel

unread,
Sep 26, 2009, 11:41:55 PM9/26/09
to
dads wrote:

> When creating a script that converts digits to words I've come across
> some unexplainable python. The script works fine until I use a 5 digit
> number and get a 'IndexError: string index out of range'. After
> looking into it and adding some print calls, it looks like a variable
> changes for no reason. The example beneath is using the digits 34567,
> the _5digit function slices 34 off and passes it to the _2digit
> function, which works with 2 digit strings but the IndexError is
> raised. Please accept my apologies for the explanation, I'm finding it
> hard to put into words. Has anyone any idea why it's acting the way it
> is?

Yeah. You convert a 5 digit number by calling _2digit for the thousands,
and _4digit for the rest. Why?

Mel.


Dave Angel

unread,
Sep 27, 2009, 12:58:30 AM9/27/09
to dads, pytho...@python.org
This program would be much simpler if you didn't use classes. So far,
they don't contribute anything but obfuscation.

Random observations:
in _2digit(), line:
if '0' in self.num

test makes no sense. Presumably what you really are trying to check is
whether the low digit of n is zero. But in fact you're checking whether
any of the 5 digits of the whole number is 0.

in _5digit(), line:


var = self._2digit(n[:2]) + ' thousand ' + self._4digit(n[2:])

should be calling self._3digit(), not self._4digit(). I presume that's
the immediate cause of your error. You'll notice that _4digit() calls
_2digit(), but by that time the problem has already been triggered.

DaveA

John Nagle

unread,
Sep 27, 2009, 2:31:25 AM9/27/09
to
dads wrote:
> Sorry forgot to mention I'm using python 2.6

This looks like a homework assignment.

John Nagle

Matt Joiner

unread,
Sep 27, 2009, 3:28:32 AM9/27/09
to John Nagle, pytho...@python.org
Yes the needless use of classes further supports that theory.

> --
> http://mail.python.org/mailman/listinfo/python-list
>

dads

unread,
Sep 27, 2009, 7:32:40 AM9/27/09
to
Thank you for the help, it's amazing what you can't spot. It seems the
harder you look the less likely you're to find the issue. Fresh eyes
make the world of difference.

To Matt and John:

No this certainly isn't homework, I'm 29 and in full time work. I
decided to learn to program about a year ago and picked up python, so
it's one of my hobbies. Starting from level 0 it's been challenging
and fun.

This exercise was just a bit of fun, I got the idea from a forum. I'm
using classes to help me solidify how they work. Unfortunately I don't
have the experience to know that this is a bad place to use them.

D'Arcy J.M. Cain

unread,
Sep 27, 2009, 11:12:11 AM9/27/09
to John Nagle, pytho...@python.org

So what? He supplied code and showed what he tried so far didn't he?
I suppose he could have said that it was homework but maybe it was a
self assigned problem to learn Python. I thought that he was doing
exactly what everyone suggested to do when they had problems with an
assignment except, maybe, telling us it was homework if it actually was.

And the answers were spot on too. Most of the help was things like
"don't use classes" and "why call _4digit." No one handed him a
complete answer on a silver platter. They made him think about it.

--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

Terry Reedy

unread,
Sep 27, 2009, 3:16:53 PM9/27/09
to pytho...@python.org
dads wrote:
> Thank you for the help, it's amazing what you can't spot. It seems the
> harder you look the less likely you're to find the issue. Fresh eyes
> make the world of difference.
>
> To Matt and John:
>
> No this certainly isn't homework, I'm 29 and in full time work. I
> decided to learn to program about a year ago and picked up python, so
> it's one of my hobbies. Starting from level 0 it's been challenging
> and fun.
>
> This exercise was just a bit of fun, I got the idea from a forum.

So it is a self-assigned homework problem ;-).
Keep at it and good luck!
We of course mostly think you made a good choice.

tjr

Message has been deleted
0 new messages