...

View Full Version : Having problems with this assignment



tridentspk
12-06-2011, 07:04 AM
Hey guys,

So I have this last question in my python lab

Its question # 6 on this page (on the right hand column):
http://goo.gl/rqDwS

My problem is figuring out how to make a loop that checks for each number between 1 and the input N.

For example, if N = 12, I'll need to print a table of numbers from 1-12 and displaying whether each number is perfect, abundant, or deficient.

So far I have only written code that determines what status the value of input N is.



def print_table(n):
i = 1
add = 0
last = n - 1
count = n
while i <= last:
if count % i == 0:
add = add + i
i = i + 1

msg = ''
if add < n:
msg = 'is deficient'
elif add == n:
msg = 'is perfect'
elif add > n:
msg = 'is abundant'

print n, msg

number = int(input("Enter a number: "))
print_table(number)


The output of the program should look like this:


Enter a number: 12
12 abundant
11 deficient
10 deficient
9 deficient
8 deficient
7 deficient
6 perfect
5 deficient
4 deficient
3 deficient
2 deficient
1 perfect

Now here is the logic behind it:

12 < 1 + 2 + 3 + 4 + 6
11 > 1
10 > 1 + 2 + 5
9 > 1 + 3
8 > 1 + 2 + 4
7 > 1
6 = 1 + 2 + 3
5 > 1
4 > 1 + 2
3 > 1
2 > 1
1 = 1

So thats why I was thinking of having some kind of while loop that produces different variables like add11, add10, add9, etc to store the sum of the numbers that leave a remainder of 0. But that doesn't seem to be possible.

Thanks for any help on this :)

Apothem
12-10-2011, 03:50 AM
Just do something like:
while i > 0:
print_table(i)
i--



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum