``````# This file contains python code that will determine whether a potivie range
# of integers entered by the user will contain odd or even numbers, prime or
# composite numbers, perfect/abundant/deficient numbers, square numbers, and
# triangular numbers.

def main():

import math

firstNum = 0
secondNum = 0
n = 0
odd = 0
prime = 0
perfect = 0
square = 0
perfect = 0
square = 0
triangular = 0

printGreeting()

firstNum = input('Please enter an integer between 1 and 100000: ')
secondNum = input('Please enter an interger greater than your pervious but not larger than 100000: ')

for n in range(firstNum, secondNum + 1):
isOdd(n)
isPrime(n)
checkForPerfect(n)
isSquare(n)
isTriangular(n)

printTablLine(n, odd, prime, perfect, square, triangular)

main()
printGreeting():
print 'This program classifies positive integers as Odd/Even,'
print 'Prime/Composite, Perfect/Abundant/Deficient, Square,'
print 'and Triangular.'
print
print 'You will now get to choose the range of positive intergers'
print 'that you would like to see classified.'
print

def isOdd(n):
if firstNum % 2 == 0:
return False
else:
return True

def isPrime(n):
if n == 2:
return True
for divisor in range(3, int(math.sqrt(n)), 2):
if n % divisor == 0:
return False
return True

def checkForPerfect(n):

sum = 0

if n %  == 0:
sum +=
if sum == n:
return 'Perfect'
elif sum > n:
return 'Abundant'
else:
return 'Abundant'
else:
return 'Deficient'

def sumDivisors(n):
# returns the sum of the divisors of n.

def isDivisor(a, b):
# a predicate function that returns True if b is a divisor of a, and False if not.

def isSquare(n):
# returns True if n is a perfect square, False if not.

def isTriangular(n):
# returns True if n is a triangular number, False if not.

def printTableLine(n, odd, prime, perfect, square, triangular):
# prints the information for one number on one line of the table.``````

This is my latest homework, could anyone help me find out the error in my isPrime() function. Also how do I set this up to print my return values, for example for isOdd() true = odd, so how would I get it to print Odd? I just need some direction, using functions and returns is confusing me.

Thanks for any input I'm a complete noob.

could anyone help me find out the error in my isPrime() function

You get a return on the first pass through the for() loop. A True if the first divide has a remainder, a False if the first divide has no remainder. You want the True after the for() loop, not within it.

``````def isPrime(n):
if n == 2:
return True
for divisor in range(3, int(math.sqrt(n)), 2):
if n % divisor == 0:
return False

## did not return 'False' during the for() loop so it is prime
return True``````

And this code will never print "Deficient". I'll let you figure out why.

``````def checkForPerfect(n):

sum = 0

if n %  == 0:
sum +=
if sum == n:
return 'Perfect'
elif sum > n:
return 'Abundant'
else:
return 'Abundant'
else:
return 'Deficient'``````

so how would I get it to print Odd

``````def isOdd(n):
if firstNum % 2 == 0:
return False
else:
return True

for num in range( 97, 100 ):
is_odd_returned = isOdd(num)
print num, "is",
if is_odd_returned == True:  ## or just if is_odd_returned:
print "Odd"
else:    ## "False' returned
print "Not odd"``````

awesome, thank you. Ya i didn't noticed i think i copied from an area i alrdy copied before i pasted. I'll come back once I work on it more when I get stuck.

thanks again

I think 2 more lines need to be added for isPrime function

``````def isPrime(n):
if n == 2:
return True

if n%2 == 0 :
return False

for divisor in range(3, int(math.sqrt(n)), 2):
if n % divisor == 0:
return False

## did not return 'False' during the for() loop so it is prime
return True``````
``````def isSquare(n):
import math
if math.sqrt(n) == :
print 'True'
else:
print 'False'``````

This is what I have so far, I need it to return True if its a perfect square, like 1, 4, 9 ect. and false if not. After finding the root how can I compare it so if it has a decimal return false. i tried

``````if math.sqrt(n) == int:
print 'True'``````

but no matter what it prints true.

also for my function isTriangular

``````def isTriangular(n):

x = (math.sqrt(8 * n + 1) - 1) / 2
if x - int(x) > 0:
return False
return True``````

returns false if not and true if it is. Can anyone explain the

x = (math.sqrt(8 * n + 1) - 1) / 2

triangular numbers have sums of consecutive ints like 1+2 = 3, 1+2+3=6 1+2+3+4 = 10 and ect.

Can anyone explain the

x = (math.sqrt(8 * n + 1) - 1) / 2

triangular numbers have sums of consecutive ints like 1+2 = 3, 1+2+3=6 1+2+3+4 = 10 and ect.

A number n is triangular if it has the form n = k(k+1)/2. For example 1+2 = 2*3/2, 1+2+3=3*4/2, 1+2+3+4=4*5/2. If you compute 8n+1, you have `8n+1 = 4k(k+1)+1 = 4 k^2 + 4k + 1 = (2k+1)^2` , so this gives `k = (sqrt(8n+1) -1)/2` .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.