problem moving from char to integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Melih Onvural

    problem moving from char to integer

    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

  • Pontus Ekberg

    #2
    Re: problem moving from char to integer

    Melih Onvural wrote:
    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.


    Comment

    • Melih Onvural

      #3
      Re: problem moving from char to integer

      This is the error message that I'm having a tough time interpreting:
      Traceback (most recent call last):
      File "pagerank.p y", line 101, in ?
      main()
      File "pagerank.p y", line 96, in main
      ch = strord(url)
      File "pagerank.p y", 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 Ekberg wrote:
      Melih Onvural wrote:
      >
      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.

      Comment

      • Larry Bates

        #4
        Re: problem moving from char to integer

        Melih Onvural wrote:
        This is the error message that I'm having a tough time interpreting:
        Traceback (most recent call last):
        File "pagerank.p y", line 101, in ?
        main()
        File "pagerank.p y", line 96, in main
        ch = strord(url)
        File "pagerank.p y", 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

        Comment

        • John Machin

          #5
          Re: problem moving from char to integer


          Melih Onvural wrote:
          This is the error message that I'm having a tough time interpreting:
          Traceback (most recent call last):
          File "pagerank.p y", line 101, in ?
          main()
          File "pagerank.p y", line 96, in main
          ch = strord(url)
          File "pagerank.p y", 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_stri ng):
          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

          Comment

          • Melih Onvural

            #6
            Re: problem moving from char to integer

            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:


            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 Machin wrote:
            Melih Onvural wrote:
            This is the error message that I'm having a tough time interpreting:
            Traceback (most recent call last):
            File "pagerank.p y", line 101, in ?
            main()
            File "pagerank.p y", line 96, in main
            ch = strord(url)
            File "pagerank.p y", 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_stri ng):
            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

            Comment

            Working...