Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Just do something like:
    while i > 0:
    print_table(i)
    i--


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •