Reputation: 363
I am trying to make a program that graphs a circle using radius and center coordinates that the user inputs. This is what I have so far
import numpy as np
import matplotlib.pyplot as plt
def circle(r,h,k,domain):
x = np.array(domain)
y = eval(np.sqrt(r**2 - (x-h)**2) + k)
plt.plot(x,y)
plt.show
rad = input("Radius: ")
xcen = int(input("Center X Coordinate: "))
ycen = int(input("Center Y Coordinate: "))
circle(rad,xcen,ycen,np.linspace(-10,10,500))
print("Done")
When I run it I get an error concerning the line y = eval(np.sqrt(r**2 - (x-h)**2) + k)
Radius: 5
Center X Coordinate: 0
Center Y Coordinate: 0
Traceback (most recent call last):
File "/Users/William/Documents/Science/PYTHON3/Circle.py", line 13, in <module>
circle(rad,xcen,ycen,np.linspace(-10,10,500))
File "/Users/William/Documents/Science/PYTHON3/Circle.py", line 5, in circle
y = eval(np.sqrt(r**2 - (x-h)**2) + k)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
>>>
What does this mean? Why is it saying str
and int
can't be used with **
Upvotes: 0
Views: 131
Reputation: 76244
rad = input("Radius: ")
In Python 3.X, the result returned by input
is a string. That string is then passed to circle
, which you try to square. But you can't square a string.
Convert rad
to a number before using it.
rad = float(input("Radius: "))
Upvotes: 3