[Tutor] why the error?
Emile van Sebille
emile@fenx.com
Tue, 30 May 2000 13:17:20 -0700
It looks like there's some kind of conversion taking
place somewhere, because if you convert your input
to an integer before invoking isprime, it all works
out OK. (see line added below)
Also, you probably want the final line in an else clause.
Right now it will always print.
Final note: if you do
for j in [2] + range(3, sqrt(number)+1,2):
you won't test all the even's after 2.
HTH,
Emile van Sebille
emile@fenx.com
-------------------
----- Original Message -----
From: Timothy Wilson <wilson@visi.com>
To: <tutor@python.org>
Sent: Tuesday, May 30, 2000 12:41 PM
Subject: [Tutor] why the error?
> Hi everyone,
>
> I've been working on some simple python programs, and I'm running into
some
> differences between the code I create in the interpretor and what's in
a
> text file. An example:
>
> I wrote a little function that determines whether a given number is
prime or
> not. (I'm pretty sure the algorithm is correct.)
>
> --snip--
> from math import *
>
> def isPrime(number):
> for j in range(2, sqrt(number)+1):
> if number % j == 0:
> return 0
> return 1
>
> if __name__ == '__main__':
> number = raw_input("Which number? ")
number = int(number)
> if isPrime(number):
> print "%i is prime." % number
> print "%i is not prime." % number
> --snip--
>
> This code seems to work perfectly when imported in the interpretor.
When I
> run it by typing 'python prime.py', I get:
>
> [wilsont@galileo python]$ python prime.py
> Which number? 11
> Traceback (innermost last):
> File "prime.py", line 15, in ?
> if isPrime(number):
> File "prime.py", line 8, in isPrime
> for j in range(2, sqrt(number)+1):
> TypeError: illegal argument type for built-in operation
>
> Any hints? Why the difference?
>
> -Tim
>
> --
> Tim Wilson | Visit Sibley online: | Check out:
> Henry Sibley HS | http://www.isd197.k12.mn.us/ | http://www.zope.org/
> W. St. Paul, MN | | http://slashdot.org/
> wilson@visi.com | <dtml-var pithy_quote> | http://linux.com/
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>