Answered You can hire a professional tutor to get the answer.

QUESTION

I wrote this code and now want to include Python's exception handling using try/except to catch the error and print out a customized error message.

I wrote this code and now want to include Python's exception handling using try/except to catch the error and print out a customized error message. I created a section called #Exception handling for this in the body of my code. Thanks for the help!

from math import *

def main():

#Creates entire main function.

   # GUI Setup

   win = GraphWin("Circle Intersection", 600, 300)

   win.setCoords(0, 0, 600, 300)

   leftScr = Rectangle(Point(0, 0), Point(300, 300))

   leftScr.setFill("peachpuff")

   leftScr.draw(win)

   for i in range(0,600,):

       Line(Point(0,i*10),Point(10,i*10)).draw(win)

   Text(Point(15,150), "0").draw(win).setSize(9)

#This is a circle function:

   Cc =Circle(Point(150,150),4)

   Cc.setFill("white")

   Cc.draw(win)

#Information Gathering from console

   Text(Point(450,280), "This Program Computes the Intersection").draw(win).setSize(9)

   Text(Point(450,260), "of a Circle with a horizontal line.").draw(win).setSize(9)

   Text(Point(400,190), "What is the Radius?: ").draw(win).setSize(9)

   Text(Point(409,160), "Define the Y-Intersect (< radius)?: ").draw(win).setSize(9)

#Input

   Radius = Entry(Point(520,190), 5)

   Radius.setText("0.0")

   Radius.draw(win)

   yInt = Entry(Point(520,160), 5)

   yInt.setText("0.0")

   yInt.draw(win)

   win.getMouse()

   y = float(yInt.getText())

   r = float(Radius.getText())

#This is input controls:

   if r<y:

       Text(Point(450,8), "ERROR: Radius < Y Intercept").draw(win).setSize(9)

       Text(Point(450,20), "Click again to Quit").draw(win).setSize(9)

       win.getMouse()

   else:

       #Output

       yIntLine = Line(Point(0,y+150), Point(300,y+150))

       yIntLine.draw(win)

       C = Circle(Point(150,150),r)

       C.draw(win)

#Adding Exception handling:

#Processing values for screen drawing output

       Xpos =  sqrt((r**2) - (y**2))

       Xneg = -1 * sqrt((r**2) - (y**2))

       Text(Point(400,100), "X int is:").draw(win).setSize(9)

       Text(Point(400,80), "-X int is:").draw(win).setSize(9)

       Text(Point(500,100), Xpos).draw(win).setSize(9)

       Text(Point(500,80), Xneg).draw(win).setSize(9)

       Cxn = Circle(Point(Xneg+150,y+150),4).draw(win).setFill("red")

       Cxp = Circle(Point(Xpos+150,y+150),4).draw(win).setFill("red")

#Click to close window

       Text(Point(450,20), "Click again to Quit").draw(win).setSize(9)

       win.getMouse()

#Closes the windew       

   win.close()

#Executes function main

main()

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question