Kinda newb-ish question

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

    Kinda newb-ish question

    What's wrong with line 8 in this code?
    x=1
    while 1==1:
    x=x+1
    y=range(1,x)
    z=0
    q=9
    for count in y:
    q=x%y
    if q==0:
    z=z+1
    if z<1:
    print x
    It keeps giving me
    Traceback (most recent call last):
    File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
    q=x%y
    TypeError: unsupported operand type(s) for %: 'int' and 'list'

    It's supposed to be a program to print out prime numbers.
    If anyone knows of a program that does this or can write one, can you
    show me the code? Thanks.
  • BW Glitch

    #2
    Re: Kinda newb-ish question

    ChocoboMog123 wrote:
    [color=blue]
    > What's wrong with line 8 in this code?
    > x=1
    > while 1==1:[/color]
    I think it would be better as:
    while True:[color=blue]
    > x=x+1
    > y=range(1,x)
    > z=0
    > q=9
    > for count in y:
    > q=x%y[/color]
    It should be:
    q=x%count[color=blue]
    > if q==0:
    > z=z+1
    > if z<1:
    > print x
    > It keeps giving me
    > Traceback (most recent call last):
    > File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
    > q=x%y
    > TypeError: unsupported operand type(s) for %: 'int' and 'list'
    >
    > It's supposed to be a program to print out prime numbers.
    > If anyone knows of a program that does this or can write one, can you
    > show me the code? Thanks.[/color]

    As it points out, y is a list.


    --
    Glitch

    -----BEGIN TF FAN CODE BLOCK-----
    G+++ G1 G2+ BW++++ MW++ BM+ Rid+ Arm-- FR+ FW-
    #3 D+ ADA N++ W OQP MUSH- BC- CN++ OM P75
    -----END TF FAN CODE BLOCK-----

    "So, give us the scoop! What's it actually feel like, bein' a Pred?"
    "Like you're three gigabytes of attitude on a two-gig hard drive. No
    wonder they got personality problems!"
    -- Cheetor and Rhinox, "Dark Designs"

    Comment

    • Duncan Smith

      #3
      Re: Kinda newb-ish question


      "ChocoboMog 123" <ChocoboMog123@ msn.com> wrote in message
      news:5c27220b.0 312111650.7f0a1 28d@posting.goo gle.com...[color=blue]
      > What's wrong with line 8 in this code?
      > x=1
      > while 1==1:
      > x=x+1
      > y=range(1,x)
      > z=0
      > q=9
      > for count in y:
      > q=x%y
      > if q==0:
      > z=z+1
      > if z<1:
      > print x
      > It keeps giving me
      > Traceback (most recent call last):
      > File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
      > q=x%y
      > TypeError: unsupported operand type(s) for %: 'int' and 'list'
      >[/color]

      The clue is in the traceback. 'y' is a list. Presumably you meant q = x %
      count.

      Duncan


      Comment

      • Hans Nowak

        #4
        Re: Kinda newb-ish question

        ChocoboMog123 wrote:[color=blue]
        > What's wrong with line 8 in this code?
        > x=1
        > while 1==1:
        > x=x+1
        > y=range(1,x)
        > z=0
        > q=9
        > for count in y:
        > q=x%y
        > if q==0:
        > z=z+1
        > if z<1:
        > print x
        > It keeps giving me
        > Traceback (most recent call last):
        > File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
        > q=x%y
        > TypeError: unsupported operand type(s) for %: 'int' and 'list'[/color]

        The error message points out what's wrong with it... you're using the %
        operator with an int (x) and a list (y), which is unsupported. You probably
        mean something like x % count, although that doesn't make the algorithm work.

        Quick tips: use range(2,x) rather than range(1,x), and x % count. An infinite
        loop is probably not a good idea either. :-)

        HTH,

        --
        Hans (hans@zephyrfal con.org)
        Memimpin Angin Perubahan Teknologi




        Comment

        • Mark Dufour

          #5
          Re: Kinda newb-ish question

          On Friday 12 December 2003 01:50, ChocoboMog123 wrote:[color=blue]
          > What's wrong with line 8 in this code?
          > x=1
          > while 1==1:
          > x=x+1
          > y=range(1,x)
          > z=0
          > q=9
          > for count in y:
          > q=x%y
          > if q==0:
          > z=z+1
          > if z<1:
          > print x
          > It keeps giving me
          > Traceback (most recent call last):
          > File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
          > q=x%y
          > TypeError: unsupported operand type(s) for %: 'int' and 'list'
          >
          > It's supposed to be a program to print out prime numbers.
          > If anyone knows of a program that does this or can write one, can you
          > show me the code? Thanks.[/color]

          Sure. The expression range(1,x) produces a list, not a number. So y is a list,
          and x%y now tries to perform a modulo operation with a list as modulo number!

          Instead of using z, you might also use a boolean, or True/False variable,
          perhaps like this:

          x=1

          while 1:
          x += 1
          prime = True
          for y in range(2,x):
          if x%y == 0:
          prime = False
          break
          if prime: print x

          Notice the (2,x) instead of (1,x). You don't want to test for division by 1..

          Of course, finding primes this way by trial division is a very slow method..
          A simple optimization would be to only try numbers up to the square root of
          x.


          Mark Dufour.
          --
          "The employment agency has selected an immature and unproven software package
          and its functionality is at the best close to Office 97," said Microsoft
          representatives .


          Comment

          • David M. Wilson

            #6
            Re: Kinda newb-ish question

            ChocoboMog123@m sn.com (ChocoboMog123) wrote...
            [color=blue]
            > It's supposed to be a program to print out prime numbers.
            > If anyone knows of a program that does this or can write one, can you
            > show me the code? Thanks.[/color]

            Have you tried Google? Python code is lovely and verbose, and that
            makes it fun to google for. Try: "def isPrime" +python




            Etc. :)


            David.

            Comment

            • Harald Massa

              #7
              Re: Kinda newb-ish question

              File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-[color=blue][color=green]
              >> q=x%y[/color][/color]
              [color=blue]
              > An infinite loop is probably not a good idea either. :-)[/color]

              Why should it not be a good idea? Are there any ideas that the number of
              primes is limited?


              :-)

              Harald

              Comment

              • ChocoboMog123

                #8
                Re: Kinda newb-ish question

                Thanks for the help everyone. Worked out fine when I changed it.

                Comment

                • Hans Nowak

                  #9
                  Re: Kinda newb-ish question

                  Harald Massa wrote:[color=blue]
                  > File "C:\Python23\Pr ime Number.py", line 8, in -toplevel-
                  >[color=green][color=darkred]
                  >>> q=x%y[/color][/color]
                  >
                  >[color=green]
                  >>An infinite loop is probably not a good idea either. :-)[/color]
                  >
                  >
                  > Why should it not be a good idea? Are there any ideas that the number of
                  > primes is limited?
                  >
                  > :-)[/color]

                  :-) No, but the program will never finish, which doesn't make it very useful
                  except for staring at its output while eating tons of pretzels and listening to
                  The Orb.

                  --
                  Hans (hans@zephyrfal con.org)
                  Memimpin Angin Perubahan Teknologi




                  Comment

                  Working...