473,237 Members | 1,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 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 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

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

Similar topics

3
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python...
3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
1
by: LRW | last post by:
I was wondering if anyone could recommend some good beginner sites and tutorial sites for writting ASP.Net pages in C#. Things that especially help with datagrids!! And, are there additional...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn...
3
by: William Foster | last post by:
Good evening all, Microsoft is really starting to annoy me as a new user. I am trying to convert my code from VBA (A very user friendly laguage with generally good help files) to Visual Studio...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
1
by: Blue_hatter | last post by:
Hey Guys, I'm a newbie to the whole C++ Programming thing as I think I said before in a post. The thing is, I have this idea that might help me to learn at a better pace than I am doing currently....
10
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.