[Tutor] Strange modulus problem
Chris Keelan
rufmetal@home.com
Thu, 11 Oct 2001 22:18:56 -0500
I'm working on a bit of code to determine all of the factors up to and
including a number entered by the user. Here's my code:
#!/usr/bin/python
import string
num=raw_input("Enter a number: ")
num_int=string.atoi(num)
factor_range= range(1, num_int + 1)
for item in factor_range:
if num_int % item > 0:
factor_range.remove(item)
print 'The factors are %s' % factor_range
But when I run it, I get the following weird output:
>>Enter a number: 6
The factors are [1, 2, 3, 5, 6]
Why doesn't '5' fail the if test and get removed? If I modify the code so
that it prints the modulus of each item, 5 correctly evalutates to '1'. What
gives?
- Chris