Я продолжаю пытаться запустить эту программу на Python, но получаю ту же ошибку операнда. Код ошибки исходит из 10-й строки - PullRequest
0 голосов
/ 05 октября 2019

Этот код не работает для меня, и я продолжаю получать ошибку неподдерживаемого типа операнда. Код ошибки читает TypeError: неподдерживаемые типы операндов для Sub: 'str' и 'str' в строке 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")

Ответы [ 3 ]

2 голосов
/ 05 октября 2019

На вход поступает строка. Вам необходимо преобразовать его в числовой формат. Пожалуйста, отметьте Как мне разобрать строку в число с плавающей точкой или int?

Кроме того, при сравнении вы должны вызывать функцию с параметрами. Я не проверял математику, но думаю, это то, что вы ищете:

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")
0 голосов
/ 11 ноября 2019
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)
0 голосов
/ 05 октября 2019

Вы должны заключить его в int() или float(), как показано ниже

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

, если вы печатаете тип переменной, который x1 делаетprint(type(x1)) Вы получите следующее:

<class 'int'>

То, как вы сейчас кодируете, вы велите программе выполнять математические операции над строкой. Вот почему вы получаете эту ошибку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...