The time now is Mon 20 May 2013, 08:48
All times are UTC - 4 |
| Author |
Message |
lwill

Joined: 13 Jun 2008 Posts: 168 Location: City Of Lights
|
Posted: Fri 20 Aug 2010, 12:24 Post subject:
Python int("1")? Subject description: Confusing Python function |
|
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: | 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: | 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:
| Quote: | 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)
|
|
Back to top
|
|
 |
neurino

Joined: 15 Oct 2009 Posts: 360
|
Posted: Mon 23 Aug 2010, 07:22 Post subject:
|
|
Note str is a reserved python word (string type) and you should NOT use it as a variable name
try:
and see where the error comes
Moreover this is your function is a bit well re-written:
| Code: |
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
|
|
Back to top
|
|
 |
lwill

Joined: 13 Jun 2008 Posts: 168 Location: City Of Lights
|
Posted: Mon 23 Aug 2010, 21:44 Post subject:
|
|
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
|
|
Back to top
|
|
 |
neurino

Joined: 15 Oct 2009 Posts: 360
|
Posted: Tue 24 Aug 2010, 03:43 Post subject:
|
|
| 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
|
|
Back to top
|
|
 |
lwill

Joined: 13 Jun 2008 Posts: 168 Location: City Of Lights
|
Posted: Tue 24 Aug 2010, 13:27 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|