473,378 Members | 1,333 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,378 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 1066
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.