user13137416
user13137416

Reputation:

how to make a radius of the circle using python

Here is my code I keep getting errors. Write a program that prompts the user to enter the coordinates of the center and a point on the circle. The program should then output the circle’s radius, diameter, circumference, and area.This is for an intro class to python.

def main():    
    x1 = eval(input("enter x1"))
    y1 = eval(input("enter y1"))
    x2 = eval(input("enter x2"))
    y2 = eval(input("enter y2"))
    print((x2-x1)**2 + (y2-y1)**2)*(1/2)

pi = 3.14 
c = float(input("input the circumference of the circle :"))
print("the diameter of the circle with circumference" + str(c) + " is: " + str(2*pi*r))

r = float(input("input the radius of the circle :"))
print("the area of the circle with radius" + str(r) + " is: " + str(pi*r^2))

print("The radius,diameter,circumference,and area")

main()

Upvotes: 0

Views: 6039

Answers (2)

azro
azro

Reputation: 54148

For circle code

  • You miss the input method, you cannot build a float from a string
  • the power operator is **
  • you can't use variable before defining them (here r)
pi = 3.14
c = float(input("input the circumference of the circle: "))
r = float(input("input the radius of the circle: "))

print("the diameter of the circle with circumference", c, "is:", str(2 * pi * r))
print("the area of the circle with radius", r, "is:", str(pi * r ** 2))

For main method

  • better use type than eval to get the type you need x1 = float(input("enter x1"))
  • you miss one closing parenthesis
def main():
    x1 = float(input("enter x1"))
    y1 = float(input("enter y1"))
    x2 = float(input("enter x2"))
    y2 = float(input("enter y2"))
    print(((x2 - x1) ** 2 + (y2 - y1)) ** 2 ** (1 / 2))
    return ((x2 - x1) ** 2 + (y2 - y1)) ** 2 ** (1 / 2)

Upvotes: 0

Óscar López
Óscar López

Reputation: 236004

There are several errors, both inside and outside the function:

1) You need to convert the input to numbers. Change this and the other similar lines:

x1 = eval(input("enter x1"))

To this:

x1 = float(input("enter x1"))

2) The ^ operator is not doing what you think, in Python we use ** as the power operator. And it's better to return a value from a function, instead of printing the result. You should replace this line:

print((x2-x1)^2+(y2-y1)^2)^(1/2)

With this:

return ((x2-x1)**2 + (y2-y1)**2)**(1/2)

3) You're using r before defining it, simply move this line to the first line:

r = float("input the radius of the circle :")

4) Change this and the other line similar to it:

c = float("input the circumference of the circle :")

To this:

c = float(input("input the circumference of the circle :"))

Upvotes: 2

Related Questions