Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2011, 06:04 AM   PM User | #1
tridentspk
New Coder

 
Join Date: Jul 2011
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
tridentspk is an unknown quantity at this point
Having problems with this assignment

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.

Code:
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:

Code:
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
tridentspk is offline   Reply With Quote
Old 12-10-2011, 02:50 AM   PM User | #2
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Just do something like:
while i > 0:
print_table(i)
i--
Apothem is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.