problem moving from char to integer

M

Melih Onvural

Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih
 
P

Pontus Ekberg

Melih said:
Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih

In what way does it not work? Please post the entire code or at least the
result of your code.
 
M

Melih Onvural

This is the error message that I'm having a tough time interpreting:
Traceback (most recent call last):
File "pagerank.py", line 101, in ?
main()
File "pagerank.py", line 96, in main
ch = strord(url)
File "pagerank.py", line 81, in strord
result[counter] = int(i);
ValueError: invalid literal for int(): i

and here is the full code:

def strord(url):
counter=0;
for i in url:
result[counter] = int(i);
counter += 1;

return result;

Thanks

Pontus said:
Melih said:
Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih

In what way does it not work? Please post the entire code or at least the
result of your code.
 
L

Larry Bates

Melih said:
This is the error message that I'm having a tough time interpreting:
Traceback (most recent call last):
File "pagerank.py", line 101, in ?
main()
File "pagerank.py", line 96, in main
ch = strord(url)
File "pagerank.py", line 81, in strord
result[counter] = int(i);
ValueError: invalid literal for int(): i

and here is the full code:

def strord(url):
counter=0;
for i in url:
result[counter] = int(i);
counter += 1;

return result;

Thanks
Says that you are trying to change the character 'i' into
an integer, which won't work. Put a print statement just
before your for loop and print out url. It doesn't just
contain numbers (as apparently you think it does).

You should also lose the semicolons, you are writing Python
now, not PHP ;-).

You might also want to write:

def strord(url):
return [int(i) for i in url]

Or just lose the function completely since it degrades to a
single list comprehension.

-Larry Bates
 
J

John Machin

Melih said:
This is the error message that I'm having a tough time interpreting:
Traceback (most recent call last):
File "pagerank.py", line 101, in ?
main()
File "pagerank.py", line 96, in main
ch = strord(url)
File "pagerank.py", line 81, in strord
result[counter] = int(i);
ValueError: invalid literal for int(): i

and here is the full code:

def strord(url):
counter=0;
for i in url:
result[counter] = int(i);
counter += 1;

return result;

You are getting this error because you are trying to convert string to
integer, but this of course complains if it finds unexpected
non-digits.
int('9') -> 9
int('x') -> this error
You may be looking for the ord() function:
ord('9') -> 57
ord('x') -> 120

In any case, once you've sorted that out, your function will die in the
same statement, because "result" isn't defined. Python isn't awk :)

Given the name of your function (strord), perhaps what you really want
is this:

def strord(any_string):
return [ord(x) for x in any_string]

but why you'd want that (unless you were trying to pretend that Python
is C), I'm having a little troubling imagining ...

Perhaps if you told us what you are really trying to do, we could help
you a little better.

HTH,
John
 
M

Melih Onvural

I'm trying to calculate the checksum of a website so that I can get its
PageRank. I'm modifying the code that I found here:
http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706

I apologize for being secretive, but I didn't mean to be. I'm trying to
take characters and push them to their integer value. Thanks for the
help and the advice on writing in Python. Definitely learning to do
things right is the goal here.

Also, the ord function worked. Thanks.

Melih

John said:
Melih said:
This is the error message that I'm having a tough time interpreting:
Traceback (most recent call last):
File "pagerank.py", line 101, in ?
main()
File "pagerank.py", line 96, in main
ch = strord(url)
File "pagerank.py", line 81, in strord
result[counter] = int(i);
ValueError: invalid literal for int(): i

and here is the full code:

def strord(url):
counter=0;
for i in url:
result[counter] = int(i);
counter += 1;

return result;

You are getting this error because you are trying to convert string to
integer, but this of course complains if it finds unexpected
non-digits.
int('9') -> 9
int('x') -> this error
You may be looking for the ord() function:
ord('9') -> 57
ord('x') -> 120

In any case, once you've sorted that out, your function will die in the
same statement, because "result" isn't defined. Python isn't awk :)

Given the name of your function (strord), perhaps what you really want
is this:

def strord(any_string):
return [ord(x) for x in any_string]

but why you'd want that (unless you were trying to pretend that Python
is C), I'm having a little troubling imagining ...

Perhaps if you told us what you are really trying to do, we could help
you a little better.

HTH,
John
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top