Shoutbox

python functions wft - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: python functions wft (/showthread.php?tid=85152)

python functions wft by Jarrod on 08-02-2008 at 11:58 PM

i'm trying to write a python function which takes the string "s" and turns in a string or integer from an English representation, for example:
five six nine
will return
569
i wrote all the code to do it, and it looks like this

code:

s = raw_input("what is the string")
s = s.replace('one','1')
s = s.replace('two','2')
s = s.replace('three','3')
s = s.replace('four','4')
s = s.replace('five','5')
s = s.replace('six','6')
s = s.replace('seven','7')
s = s.replace('eight','8')
s = s.replace('nine','9')
s = s.replace('zero','0')
s = s.replace(' ','')
s = s.strip()
s = s
print s

now when i turn it into a function is should look like this
code:


s = raw_input("what is the string")

def words2number(s):

    while 1:

        s = s.replace('one','1')
        s = s.replace('two','2')
        s = s.replace('three','3')
        s = s.replace('four','4')
        s = s.replace('five','5')
        s = s.replace('six','6')
        s = s.replace('seven','7')
        s = s.replace('eight','8')
        s = s.replace('nine','9')
        s = s.replace('zero','0')
        s = .replace(' ','')
        s = s.strip()
        s = s

        return s

words2number(s)


but my return does not work can someone explain the technicalities of that to me?

RE: python functions wft by segosa on 08-03-2008 at 01:37 AM

I don't know python at all but why is the function inside a while loop that'll continue forever?


RE: python functions wft by Jarrod on 08-03-2008 at 01:53 AM

why not?
the return will act if a break if it works correctly anyway


quote:
Originally posted by Jarrod

   
code:

    s = raw_input("what is the string")

    def words2number(s):

        while 1:

            s = s.replace('one','1')
            s = s.replace('two','2')
            s = s.replace('three','3')
            s = s.replace('four','4')
            s = s.replace('five','5')
            s = s.replace('six','6')
            s = s.replace('seven','7')
            s = s.replace('eight','8')
            s = s.replace('nine','9')
            s = s.replace('zero','0')
            s = .replace(' ','')
            s = s.strip()
            s = s

            return s

    words2number(s)



z = words2number(s)
print z

stop spamming you 2
RE: python functions wft by ShawnZ on 08-03-2008 at 10:01 AM

quote:
Originally posted by Jarrod
why not?

because a loop that goes forever... doesn't end?
RE: python functions wft by Mnjul on 08-03-2008 at 10:07 AM

There's a return inside the loop so the loop breaks. However that loop statement is indeed necessary.. you don't need the "while 1:", Jarrod :p

And you're missing an s here:
s = .replace(' ','')


RE: python functions wft by Jarrod on 08-03-2008 at 11:14 AM

i am missing an s the return does break the loop and tbh the loop isn't necessary but it's there to make sure, i did it without the loop in the end by as i said above z = words2numbers(s)

and ShawnZ didn't you read my post the return breaks the loop


RE: python functions wft by foaly on 08-03-2008 at 11:54 AM

quote:
Originally posted by Jarrod
and ShawnZ didn't you read my post the return breaks the loop
should break the loop....if it doesn't the program will probably crash...