473,237 Members | 1,214 Online

# Beginner's Help

I am new to programming in python after programming in Java, C and C# for a number of years. Somehow I'm missing something or am doing something wrong, but this Pythagorean theorem program is giving me wrong answers.

Expand|Select|Wrap|Line Numbers
1. print "Pythagorean Solver"
2. print "Enter -1 for unknown values."
3.
4. A = input("A? ")
5. B = input("B? ")
6. C = input("C? ")
7.
8. if A == -1:
9.     if B == -1:
10.         print "Too many unknowns."
11.         sys.exit()
12.     if C == -1:
13.         print "Too many unknowns."
14.         sys.exit()
15.     else:
16.         D = (C**2-B**2)**(1/2)
17.         print "A = ",D
18.
19. if B == -1:
20.     if A == -1:
21.         print "Too many unknowns."
22.         sys.exit()
23.     if C == -1:
24.         print "Too many unknowns."
25.         sys.exit()
26.     else:
27.         D = (C**2-A**2)**(1/2)
28.         print "B = ", D
29.
30. if C == -1:
31.     if A == -1:
32.         print "Too many unknowns."
33.         sys.exit()
34.     if B == -1:
35.         print "Too many unknowns."
36.         sys.exit()
37.     else:
38.         D = (A**2 + B**2)**(1/2)
39.         print "C = ", D
40.
41. else:
42.     print "Nothing to solve."
43.
Jul 12 '07 #1
1 1061
bvdet
2,851 Expert Mod 2GB
I am new to programming in python after programming in Java, C and C# for a number of years. Somehow I'm missing something or am doing something wrong, but this Pythagorean theorem program is giving me wrong answers.

Expand|Select|Wrap|Line Numbers
1. print "Pythagorean Solver"
2. print "Enter -1 for unknown values."
3.
4. A = input("A? ")
5. B = input("B? ")
6. C = input("C? ")
7.
8. if A == -1:
9.     if B == -1:
10.         print "Too many unknowns."
11.         sys.exit()
12.     if C == -1:
13.         print "Too many unknowns."
14.         sys.exit()
15.     else:
16.         D = (C**2-B**2)**(1/2)
17.         print "A = ",D
18.
19. if B == -1:
20.     if A == -1:
21.         print "Too many unknowns."
22.         sys.exit()
23.     if C == -1:
24.         print "Too many unknowns."
25.         sys.exit()
26.     else:
27.         D = (C**2-A**2)**(1/2)
28.         print "B = ", D
29.
30. if C == -1:
31.     if A == -1:
32.         print "Too many unknowns."
33.         sys.exit()
34.     if B == -1:
35.         print "Too many unknowns."
36.         sys.exit()
37.     else:
38.         D = (A**2 + B**2)**(1/2)
39.         print "C = ", D
40.
41. else:
42.     print "Nothing to solve."
43.
Instead of '1/2' for the power, use '0.5' or '1/2.0'. You were performing integer division. Look at this example:
Expand|Select|Wrap|Line Numbers
1. >>> 100**(1/2)
2. 1
3. >>> 1/2
4.
5. >>> 100**0
6. 1
7. >>> 100**0.5
8. 10.0
9. >>>
Jul 12 '07 #2