#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    My first program! Need some advice


    Hello everyone,

    I'm starting to learn Python on my own as my first programming language and I'm using the book "Introduction to Computation and Programming Using Python", by John Guttag.

    This is the first task of the book: "Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect."

    After a few hours of work (wasn't so easy at the start) this is the final version of the program. I've done some tests and, until now, it doesn't appear to have bugs, but I need a avaliation of someone that have more experience with programming and some advices on how to improve it.

    Sorry about my poor english, it isn't my native language (I'm from Brazil).

    Thanks for the help!

    Code:
    #Asks the user to enter a integer(x) and prints two integer (root, power),
    #such that root**power= x
    #0 < power < 6
    
    x = int(raw_input('Enter a integer: '))
    
    # x == 1 (special case) 
    if x == 1:
        i=1
        while i<6:
            print '(0, ' + str(i) + ')'
            i += 1
        print '(1, 1)'
    
        
    
    # x == 0 (special case)
    elif x == 0:
        print "Doesn't exist a pair of integer 'a' and 'b' where a**b = " + str(x)
    
    
    
    # x < 0
    elif x<0:
        pwr = 1
        while pwr<6:        
            root = x
            while root < 0:                
                if root**pwr == x:
                    print '(' + str(root) + ', ' + str(pwr) + ')'
                    root = root + 1
                else:
                    root = root + 1
            pwr = pwr + 1
    
            
    
    # x > 0
    else:
        pwr = 1
        while pwr<6:        
            root = x
            while root > 0:                
                if root**pwr == x:
                    print '(' + str(root) + ', ' + str(pwr) + ')'
                    if pwr %2 == 0:
                         print '(' + str(-root) + ', ' + str(pwr) + ')'
                         root = root - 1                     
                    else:
                        root = root - 1
                        pwr = pwr + 1
                else:
                    root = root - 1
            pwr = pwr + 1
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Maybe:
    python Code:
    def root_pow():
        while 1:
            try:
                num = int(raw_input("Enter an integer: "))
                break
            except ValueError:
                print("I said an integer... sigh.")
     
        solutions = []
        for power in range(1,6):
            root = abs(num)**(1.0/power)
            if root == int(root):
                if num>=0:
                    if num and not power%2:
                        solutions.append((-int(root),power))
                    solutions.append((int(root),power))
                elif num<0 and power%2:
                    solutions.append((-int(root),power))
     
        if solutions:
            for r,p in solutions:
                print("Root = {} , Power = {}".format(r,p))
        else:
            print("Sorry, no such pair found (isn't that strange).")
     
    if __name__ == "__main__":
        root_pow()

    -Mek

    Comments on this post

    • Daniel Dantas agrees
    Last edited by Mekire; April 19th, 2013 at 03:31 AM. Reason: Minor change to code (gives negative roots)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Combine the best features of this code (pwr_skip and sign) with the best features of mek's code (all the rest---the function, isolation of print statements, input verification, and all the other good qualities I've overlooked) and you'll have a worthy code. "Avaliation" is the prettiest word I'll see this year.
    Code:
    x = int(raw_input('Enter a integer: '))
    
    # x == 0 (special case)
    if x == 0:
        print "Doesn't exist a pair of integer 'a' and 'b' where a**b = " + str(x)
    
    # x == 1 (special case)
    elif x == 1:
        for i in range(1,6):
            print '(0, ' + str(i) + ')'
        print '(1, 1)'
    
    else:
        if x<0:
            pwr_skip, sign = 2, -1
        else:
            pwr_skip, sign = 1, 1
        for pwr in range(1, 6, pwr_skip):
            root = sign * int(round(abs(x)**(1.0/pwr)))
            if root**pwr == x:
                print '(' + str(root) + ', ' + str(pwr) + ')'
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Thank you for support guys! These are pretty good advices. As I'm starting now, I don't master the function feature, but I did understand what you proposed.

    And yeah, I mixed "evaluation" with "avaliação" (the corresponding word in portuguese) LOL... It's embarrassing, but I think you realized what I was trying to say.

    Thank you again!

IMN logo majestic logo threadwatch logo seochat tools logo