Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 18th, 2013, 11:45 PM
 Daniel Dantas
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 18 m 18 sec
Reputation 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
April 19th, 2013, 03:00 AM
 Mekire
Contributing User

Join Date: Oct 2012
Posts: 171
Time spent in forums: 3 Days 8 h 5 m 26 sec
Reputation Power: 2
Maybe:
python Code:
 Original - 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
Daniel Dantas agrees!

Last edited by Mekire : April 19th, 2013 at 04:31 AM. Reason: Minor change to code (gives negative roots)

#3
April 19th, 2013, 10:16 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,135
Time spent in forums: 1 Month 3 Weeks 2 Days 6 h 18 m 28 sec
Reputation Power: 455
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!

#4
April 19th, 2013, 10:29 AM
 Daniel Dantas
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 18 m 18 sec
Reputation 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!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > My first program! Need some advice