471,310 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

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 1015
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by jvax | last post: by
8 posts views Thread by Grrrbau | last post: by
1 post views Thread by LRW | last post: by
14 posts views Thread by z_learning_tester | last post: by
3 posts views Thread by William Foster | last post: by
10 posts views Thread by See_Red_Run | last post: by
1 post views Thread by Blue_hatter | last post: by
10 posts views Thread by hamza612 | last post: by
22 posts views Thread by ddg_linux | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.