What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » python functions wft

python functions wft
Author: Message:
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
O.P. Undecided  python functions wft
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?

This post was edited on 08-03-2008 at 12:00 AM by Jarrod.

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














08-02-2008 11:58 PM
Profile E-Mail PM Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: python functions wft
I don't know python at all but why is the function inside a while loop that'll continue forever?
The previous sentence is false. The following sentence is true.
08-03-2008 01:37 AM
Profile PM Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
O.P. RE: python functions wft
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

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














08-03-2008 01:53 AM
Profile E-Mail PM Find Quote Report
ShawnZ
Veteran Member
*****

Avatar

Posts: 3146
Reputation: 43
32 / Male / Flag
Joined: Jan 2003
RE: python functions wft
quote:
Originally posted by Jarrod
why not?

because a loop that goes forever... doesn't end?
Spoiler:
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
08-03-2008 10:01 AM
Profile PM Web Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: python functions wft
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(' ','')

This post was edited on 08-03-2008 at 10:08 AM by Mnjul.
08-03-2008 10:07 AM
Profile PM Web Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
O.P. RE: python functions wft
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

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














08-03-2008 11:14 AM
Profile E-Mail PM Find Quote Report
foaly
Senior Member
****

Avatar

Posts: 718
Reputation: 20
38 / Male / Flag
Joined: Jul 2006
RE: python functions wft
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...
08-03-2008 11:54 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On