0

This code is not working for me, and I keep getting an unsupported operand type error. The error code reads TypeError: unsupported operand type(s) for Sub: 'str' and 'str' on line 10

x1 = input(print("What is the x coordinate of the first circle: "))
y1 = input(print("What is the y coordinate of the first circle: "))
r1 = input(print("What is the radius of the first circle: " ))
x2 = input(print("What is the x coordinate of the second circle: "))
y2 = input(print("What is the y coordinate of the second circle: "))
r2 = input(print("What is the radius of the second circle: "))

import math
def distance(x1, y1, x2, y2):
    return math.sqrt(math.pow(x2 - x1, 2) +
            math.pow(y2 - y1, 2) * 1.0) 
print("%.6f"%distance(x1, y1, x2, y2)) 

if distance <= abs(r1 - r2):
    print("Circle 2 is inside of circle 1")
elif distance <= r1 + r2:
    print("Circle 2 overlaps circle 1")
else:
    print("Circle 2 does not overlap circle 1")
  • Possible duplicate of [TypeError: unsupported operand type(s) for /: 'str' and 'str'](https://stackoverflow.com/questions/15235703/typeerror-unsupported-operand-types-for-str-and-str), and [TypeError: unsupported operand type(s) for -: 'str' and 'str' in python](https://stackoverflow.com/questions/53603948/typeerror-unsupported-operand-types-for-str-and-str-in-python) and [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers). – wwii Oct 05 '19 at 02:30
  • Searching with the error message will often point you in the right direction. – wwii Oct 05 '19 at 02:31
  • 1
    When asking about code that produces an Exception, always include the complete Traceback in the question. Copy the Traceback and paste it in the question, then format it as code (select it and type ctrl-k) – wwii Oct 05 '19 at 02:32

3 Answers3

2

The input receives a string. You need to convert it to a number format. Please check How do I parse a string to a float or int?

Also, when you compare, you have to call the function with parameters. I did not check the math, but I guess this is what you are looking for:

y1 = float(input(print("What is the y coordinate of the first circle: ")))
r1 = float(input(print("What is the radius of the first circle: " )))
x2 = float(input(print("What is the x coordinate of the second circle: ")))
y2 = float(input(print("What is the y coordinate of the second circle: ")))
r2 = float(input(print("What is the radius of the second circle: ")))

import math
def distance(x1, y1, x2, y2):
    return math.sqrt(math.pow(x2 - x1, 2) +
            math.pow(y2 - y1, 2) * 1.0)
print("%.6f"%distance(x1, y1, x2, y2))

if distance(x1, y1, x2, y2) <= abs(r1 - r2):
    print("Circle 2 is inside of circle 1")
elif distance(x1, y1, x2, y2) <= (r1 + r2):
    print("Circle 2 overlaps circle 1")
else:
    print("Circle 2 does not overlap circle 1")
xiaxio
  • 631
  • 1
  • 10
  • 15
0

You have to enclose it with int() or float(), as shown below

x1 = int(input(print("What is the x coordinate of the first circle: ")))

if you print out the type of variable that x1 is by doing print(type(x1)) You'll get the following:

<class 'int'>

The way you have you code right now, you're telling the program to do math on a string. Which is why you get that error.

BMStacks
  • 56
  • 7
0
y1 = int(input("What is the y coordinate of the first circle : "))
r1 = int(input("What is the radius of the first circle       : "))
x2 = int(input("What is the x coordinate of the second circle: "))
y2 = int(input("What is the y coordinate of the second circle: "))
r2 = int(input("What is the radius of the second circle      : "))
x=y1*r1*x2*y2*r2
print(x)