Este código no me funciona y sigo recibiendo un error de tipo de operando no compatible. El código de error dice TypeError: tipos de operando no admitidos para Sub: 'str' y 'str' en la línea 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 respuestas
La entrada recibe una cadena. Necesita convertirlo a un formato de número. Compruebe ¿Cómo analizo una cadena en un flotante o int?
Además, cuando compara, debe llamar a la función con parámetros. No revisé las matemáticas, pero supongo que esto es lo que estás buscando:
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")
Debe incluirlo con int()
o float()
, como se muestra a continuación
x1 = int(input(print("What is the x coordinate of the first circle: ")))
Si imprime el tipo de variable que x1
es haciendo print(type(x1))
obtendrá lo siguiente:
<class 'int'>
De la forma en que tiene su código en este momento, le está diciendo al programa que haga cálculos matemáticos en una cadena. Es por eso que obtienes ese error.
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)
Preguntas relacionadas
Preguntas vinculadas
Nuevas preguntas
python
Python es un lenguaje de programación multipropósito, de tipificación dinámica y de múltiples paradigmas. Está diseñado para ser rápido de aprender, comprender y usar, y hacer cumplir una sintaxis limpia y uniforme. Tenga en cuenta que Python 2 está oficialmente fuera de soporte a partir del 01-01-2020. Aún así, para preguntas de Python específicas de la versión, agregue la etiqueta [python-2.7] o [python-3.x]. Cuando utilice una variante de Python (por ejemplo, Jython, PyPy) o una biblioteca (por ejemplo, Pandas y NumPy), inclúyala en las etiquetas.