简体   繁体   中英

I keep trying to run this program on Python, but I keep getting the same operand error. The error code is coming from the 10th line

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")

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")

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.

You need to convert the string that input() returns to a number.

eg

variable=int(input("test---"))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM