converting an int to a string

Discussion in 'Python' started by Sean Farrow, Jun 8, 2007.

  1. Sean Farrow

    Sean Farrow Guest

    Hi:
    I have the folling code:
    def parseTime(self, time):
    minutes =int(float(time)/60)
    seconds =int(float(time)-minutes*60)
    minutes =str(minutes)
    seconds =str(minutes)
    the statements that convert the minutes and seconds variables
    str(minutes) and str(seconds) don't seem to be working.
    Any idea why?
    is there any other way of doing this and perhaps using the $d
    interpolation operator?
    sean.
     
    Sean Farrow, Jun 8, 2007
    #1
    1. Advertising

  2. Sean Farrow schrieb:
    > Hi:
    > I have the folling code:
    > def parseTime(self, time):
    > minutes =int(float(time)/60)
    > seconds =int(float(time)-minutes*60)
    > minutes =str(minutes)
    > seconds =str(minutes)
    > the statements that convert the minutes and seconds variables
    > str(minutes) and str(seconds) don't seem to be working.
    > Any idea why?
    > is there any other way of doing this and perhaps using the $d
    > interpolation operator?


    What do you mean by "don't seem to be working"

    Usually, they do:

    Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    Welcome to rlcompleter2 0.96
    for nice experiences hit <tab> multiple times
    >>> str(120)

    '120'
    >>>


    Any change you overdefined str with something weird?

    Diez
     
    Diez B. Roggisch, Jun 8, 2007
    #2
    1. Advertising

  3. Sean Farrow

    Paul McGuire Guest

    On Jun 8, 10:40 am, Sean Farrow <> wrote:
    > Hi:
    > I have the folling code:
    > def parseTime(self, time):
    > minutes =int(float(time)/60)
    > seconds =int(float(time)-minutes*60)
    > minutes =str(minutes)
    > seconds =str(minutes)
    > the statements that convert the minutes and seconds variables
    > str(minutes) and str(seconds) don't seem to be working.
    > Any idea why?
    > is there any other way of doing this and perhaps using the $d
    > interpolation operator?
    > sean.


    The divmod builtin function is your friend here.

    def parseTime(time):
    seconds = float(time)
    minutes,seconds = divmod(seconds,60)
    return "%02d:%06.3f" % (minutes,seconds) # just guessing

    divmod divides the first argument by the second, and returns the tuple
    (quotient,remainder).

    -- Paul
     
    Paul McGuire, Jun 8, 2007
    #3
  4. Sean Farrow

    John Machin Guest

    On Jun 9, 1:40 am, Sean Farrow <> wrote:
    > Hi:
    > I have the folling code:
    > def parseTime(self, time):
    > minutes =int(float(time)/60)
    > seconds =int(float(time)-minutes*60)
    > minutes =str(minutes)
    > seconds =str(minutes)


    seconds = str(minutes) ???
    Is that the actual code that you executed?
    What does "don't seem to be working" mean? The seconds output is
    always the same as the minutes output?
    Try again, with
    seconds = str(seconds)
    followed by
    print "debugging", repr(time), repr(minutes), repr(seconds)
    If it still seems not to be working, come back with a copy/paste of
    the actual code that you executed, plus a copy/paste of the output

    > the statements that convert the minutes and seconds variables
    > str(minutes) and str(seconds) don't seem to be working.
    > Any idea why?
    > is there any other way of doing this and perhaps using the $d
    > interpolation operator?


    What is the $d interpolation operator? Do you mean the %d string
    formatting operator? If so, the answer depends on what you want to do
    with the output ... e.g.
    "%d:%02d" % (int_minutes, int_seconds)
    might be what you want.
     
    John Machin, Jun 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,223
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  4. Replies:
    6
    Views:
    1,532
    Richard Tobin
    Mar 19, 2009
  5. jdm
    Replies:
    1
    Views:
    665
    Victor Bazarov
    May 18, 2010
Loading...

Share This Page