1

I essentially need to make a simple calculator, that, when two sides are entered, it produces the third. I'm having a bit of a problem with the sqrt function. This is what I have so far

import math

print("It's triangle fun time! Enter the numbers you know, and a ? for the number you don't.")

sideA = int(input("Side A: "))
sideB = int(input("Side B: "))
sideC = int(input("Hypotenuse: "))

if sideA == ("?"):
    print("Side A =", sideC ** 2 - sideB**2)

elif sideB == ("?"):
    print("Side B =", sideC ** 2 - sideA**2)

elif sideC == ("?"):
    print("Side C =", math.sqrt(sideA)**2 + (sideB)**2)

Any help would be much appreciated!

Edit - code updated

(there are not spaces between sideC and ** and 2, but it was bolding it so I changed it)

Dinesh Suthar
  • 148
  • 2
  • 12
mememan9001
  • 31
  • 1
  • 8
  • 1
    you do not need to write `str("?")`, it's already a string – yash Oct 26 '17 at 01:31
  • Changed, thank you – mememan9001 Oct 26 '17 at 01:34
  • You should also [*never* use `is` for equality testing](https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce#1504742); the fact that it works here is an implementation detail of CPython and subject to change. – trent Oct 26 '17 at 01:36
  • I didn't think I was meant to but I forgot about == and = wasn't working haha – mememan9001 Oct 26 '17 at 01:37
  • Better link than that one: https://stackoverflow.com/a/2988117/3650362 – trent Oct 26 '17 at 01:38
  • Thank you, that's very helpful! – mememan9001 Oct 26 '17 at 01:39
  • Incidentally there is also the [`math.hypot`](https://docs.python.org/3.6/library/math.html#math.hypot) function. Only solves part of your problem though. – trent Oct 26 '17 at 01:44

5 Answers5

4

I am assuming you are using Python 3.

  • The input() function returns a string, not an integer. You will need to convert it from a string to an integer after you have checked that it isn't a ? input. To do this, use int()

  • Don't use is for comparison, use ==

  • Did you import the sqrt function from the math module?

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • I changed those things, however I'm still having this problem when I try to import sqrt, and if I don't it says I need to Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 2, in math.sqrt() TypeError: sqrt() takes exactly one argument (0 given) – mememan9001 Oct 26 '17 at 01:34
  • @mememan9001 The code you have shown above does not reflect this then. When you use `math.sqrt()`, you need to add in a number - an argument to the function - (eg: `math.sqrt(4)` will return 2) – TerryA Oct 26 '17 at 01:35
  • I've changed the last line to be print("Side C =", math.sqrt(sideA **2 + sideB **2)) But I still get Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 16, in print("Side C =", math.sqrt(sideA **2 + sideB **2)) TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' – mememan9001 Oct 26 '17 at 01:40
  • Yes - use `int()` to convert a string from an integer to avoid TypeErrors. Apologies, I didn't make that clear in my answer – TerryA Oct 26 '17 at 01:41
  • Okay I think I know what you're saying but I'm having trouble actually doing it. I changed the (input) to be (int(input)) for the sides but now I'm getting this Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 7, in sideC = int(input("Hypotenuse: ")) ValueError: invalid literal for int() with base 10: '?' – mememan9001 Oct 26 '17 at 01:46
  • @mememan9001 Yes. Convert it to an integer *after* you check for `?`. So do `print("Side C =", math.sqrt(int(sideA)**2 + int(sideB)**2))`, where A and B are not question marks – TerryA Oct 26 '17 at 01:49
  • I'm still having the same error as above, and I typed exactly what you just did – mememan9001 Oct 26 '17 at 01:51
  • @mememan9001 And was your input two real numbers and a ? for A/B/C ? – TerryA Oct 26 '17 at 01:53
  • Yep, every time I've been doing two numbers and a question mark, usually 3, 5, ?, but mixing it up to test every now and then – mememan9001 Oct 26 '17 at 01:54
  • Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 7, in sideC = int(input("Hypotenuse: ")) ValueError: invalid literal for int() with base 10: '?' – mememan9001 Oct 26 '17 at 01:57
  • @mememan9001 Remove the `int()` around all inputs since you don't know whether the input will be of integer format or not – TerryA Oct 26 '17 at 01:58
  • @mememan9001 Edit your question and post the full code you have right now – TerryA Oct 26 '17 at 02:00
1

Have you imported math in order to use sqrt()?

import math
math.sqrt(4)
trent
  • 25,033
  • 7
  • 51
  • 90
  • Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 2, in math.sqrt() TypeError: sqrt() takes exactly one argument (0 given) – mememan9001 Oct 26 '17 at 01:32
  • You put what you want to take the square root of inside the 'math.sqrt()' if you leave the () empty you will get an error. – Alexander Larios Oct 26 '17 at 01:36
0

Math in ℝ is in the math module.

Correspondingly, math in ℂ is in cmath.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0
import math
# Your code here
math.sqrt( sideA **2 + sideB **2 )
kader
  • 24
  • 1
  • 4
  • Traceback (most recent call last): File "C:\Python34\Problem 5.py", line 16, in print("Side C =", math.sqrt( sideA **2 + sideB **2 )) TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' – mememan9001 Oct 26 '17 at 01:36
  • As stated above, input() function returns a string, not a number. So, you need to convert your input into number values. You can try: numA = float(sideA), numB = float(sideB). And then numC = math.sqrt(numA **2 + numB **2 ) – kader Oct 26 '17 at 01:41
0

I got it!

import math

print("It's triangle fun time! Enter the numbers you know, and a '0' for the number you don't.")

sideA = (int(input("Side A: ")))

sideB = (int(input("Side B: ")))

sideC = (int(input("Hypotenuse: ")))

if sideA is 0:

sideA = math.sqrt((sideC**2) - (sideB**2))
print("Side A =", sideA)

if sideB is 0:

sideB = math.sqrt((sideC**2) - (sideA**2))
print("Side B =", sideB)

if sideC is 0:

sideC = math.sqrt((sideA**2) + (sideB)**2)
print("Side C =", sideC)
mememan9001
  • 31
  • 1
  • 8