Python int("1")?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
lwill
Posts: 171
Joined: Fri 13 Jun 2008, 04:00
Location: City Of Lights
Contact:

Python int("1")?

#1 Post by lwill »

Struggling to understand Python.
I am trying to run a script from http://aug.ment.org/dvd/makespumux.py used to convert subtitles to graphics.
The file I am reading looks like:

Code: Select all

1
00:00:20,000 --> 00:00:24,400
Altocumulus clouds occur between six thousand
 
2 
00:00:24,600 --> 00:00:27,800 
and twenty thousand feet above ground level.
The section of the code I am having problems with is:
snip

Code: Select all

def IsInt( str ):
    """ Is the given string an integer? """
    ok = 1
    try:
        num = int(str)
    except ValueError:
        ok = 0
    return ok
snip
***It does not catch the first "1" in the file!!!

From the Python manual:
int([x[, base]])¶

Convert a string or number to a plain integer. If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace. The base parameter gives the base for the conversion (which is 10 by default) and may be any integer in the range [2, 36], or zero. If base is zero, the proper radix is determined based on the contents of string; the interpretation is the same as for integer literals. (See Numeric literals.) If base is specified and x is not a string, TypeError is raised. Otherwise, the argument may be a plain or long integer or a floating point number. Conversion of floating point numbers to integers truncates (towards zero). If the argument is outside the integer range a long object will be returned instead. If no arguments are given, returns 0.

Sooo... My question is: How can you check to see if the character "1" is an int or not if the command only accepts "the range [2, 36], or zero"

If I put a blank line at the beginning of the file, it works. (?)

Is there a reason "1" is different, and is there a trick to testing it?

Thanks
Lyle

(I hate registering for a bunch of sites, so since everyone here is so helpfull I thought I would ask here first)

User avatar
neurino
Posts: 362
Joined: Thu 15 Oct 2009, 13:08

#2 Post by neurino »

Note str is a reserved python word (string type) and you should NOT use it as a variable name

try:

Code: Select all

int(str)
and see where the error comes

Moreover this is your function is a bit well re-written:

Code: Select all

def IsInt( s ): 
    """ Is the given string an integer? """ 
    try: int(s) 
    except ValueError: return False
    return True
If you still experience "1" not being recognized then look for the error ouside IsInt function

Cheers

User avatar
lwill
Posts: 171
Joined: Fri 13 Jun 2008, 04:00
Location: City Of Lights
Contact:

#3 Post by lwill »

Thank you for the response.
The code I listed was straight from the source, which had supposedly worked for the author.
I was not aware of the reserved word or I would have tried changing it.
I found another work around in the mean time (using a separate counter instead of checking the string) and can now do a lot more with the script.

I still don't understand :
"may be any integer in the range [2, 36], or zero"
Why not 1?

Thank you,
Lyle

User avatar
neurino
Posts: 362
Joined: Thu 15 Oct 2009, 13:08

#4 Post by neurino »

lwill wrote: "may be any integer in the range [2, 36], or zero"
That sentence is about base parameter so 10 is about decimal and '11' converts to decimal 11, 2 is about binary so '11' converts to decimal 3 and so on... What if zero is a special case, I guess '0x10' is read as exadecimal so results in decimal 16 and so on

User avatar
lwill
Posts: 171
Joined: Fri 13 Jun 2008, 04:00
Location: City Of Lights
Contact:

#5 Post by lwill »

Aaahh!
Thank you again!
Speed reading got the better of me and I did not make the connection that it was referring to the base of the input and not the input itself.

Post Reply