[英]Traceback Error in Python upon Second Run of code
我目前正在编写一个三角形计算器,该计算器可以根据用户的输入求解三角形,并使用Turtle绘制三角形。 但是,尽管它在第一个三角形中正常运行,但是当代码重复其自身并询问用户是否要绘制另一个三角形时,我遇到了一个错误。
这是我到目前为止的代码:
import math
import turtle
def ImpossibleTriangle():
print ""
print("That Triangle is imposssible, please try again: ")
print ""
return SSS()
def AngleLarge():
AngleSize = int(input("That angle is too large, Try again: "))
return AngleSize
def AngleZero():
AngleSize = int(input("The angle cannot be zero, Try again: "))
return AngleSize
def SideZero():
ZeroLength = int(input("The length cannot be zero, Try again: "))
return ZeroLength
def Sine(side, angleA, angleC):
ExtraSide = abs(side*(math.sin(math.degrees(angleA)))/math.sin(math.degrees(angleC)))
return ExtraSide # 'abs' used incase inputted values are negative
# Triangle given two angles and a side not between them
def AAS():
print ""
angleA = int(input("Please enter your first angle: "))
while angleA>178: # Angle cannot be greater than 178 because this
angleA = AngleLarge() # means that the other angles are not int but are floats
while angleA == 0:
angleA = AngleZero()
else:
angleC = int(input("Please enter your second angle: "))
while (angleA+angleC)>179: # Again if it is >179 angleB will be a float
angleC = AngleLarge()
while angleC == 0:
angleC = AngleZero()
angleB = (180-(angleA + angleC)) # Calculates remaining angle
sidec = int(input("Please enter the known side: "))
sidea = Sine(sidec, angleA, angleC)
sideb = Sine(sidec, angleB, angleC)
sketch(sidea, sideb, sidec, angleA, angleB, angleC)
# Triangle with two angles and a side between them
def ASA():
print ""
angleA = int(input("Please enter you first angle: "))
while angleA>=178:
angleA = AngleLarge()
while angleA == 0:
angleA = AngleZero()
angleB = int(input("Please enter your second angle: "))
while (angleA + angleB) > 178:
angleB = AngleLarge()
while angleB == 0:
angleB = AngleZero()
angleC = (180-(angleA + angleB))
sidec = int(input("Please enter the known side: "))
while sidec == 0:
sidec = SideZero()
sidea = Sine(sidec, angleA, angleC)
sideb = Sine(sidec, angleB, angleC)
sketch(sidea, sideb, sidec, angleA, angleB, angleC)
# Triangle with only sides
def SSS():
print ""
sidea = int(input("Please enter your first side: "))
while sidea == 0:
sidea = SideZero()
sideb = int(input("Please enter your second side: "))
while sideb == 0:
sideb = SideZero()
sidec = int(input("Please enter your third side: "))
while sidec == 0:
sidec = SideZero()
while sidea + sideb <= sidec or sideb + sidec <= sidea or sidea + sidec <= sideb:
sidea, sideb, sidec = ImpossibleTriangle()
angleA = int(math.degrees(math.acos(float(((sideb*sideb)+(sidec*sidec)-(sidea*sidea)))/float((2*sideb*sidec)))))
angleB = int(math.degrees(math.acos(float(((sidec*sidec)+(sidea*sidea)-(sideb*sideb)))/float((2*sidec*sidea)))))
angleC = (180-(angleA + angleB))
sketch(sidea, sideb, sidec, angleA, angleB, angleC)
# Stating all values calculated, telling the user all information:
def sketch(sidea, sideb, sidec, angleA, angleB, angleC):
sidea = round(sidea,1) # These lines round the decimal place
sideb = round(sideb,1) # of each of the known values to 'x'.
sidec = round(sidec,1) # If the values were not rounded then
print ""
print("These are the sides of your Triangle: - a, b, c -")
print(sidea, sideb, sidec)
angleA = round(angleA,1) # it would result in the code printing
angleB = round(angleB,1) # a longer string that we desire
angleC = round(angleC,1) # it can be altered by changing the value. "1"
print ""
print("These are the angles of your Triangle: - A, B, C -")
print(angleA, angleB, angleC)
# Drawing each line (direction and distance)
TD = turtle.Turtle() # TD = Triangle Drawing
TD.speed(1) # This isn't important, though it can be used later
# in the GUI if desired...
TD.forward(sidea)
TD.left(180-angleC)
TD.forward(sideb)
TD.left(180-angleA)
TD.goto(0,0)
turtle.Screen().exitonclick()
return home2()
# Home Page
def Terminate():
Quit = raw_input("Are you sure you want to quit? ")
if Quit == "Yes" or Quit == "yes" or Quit == "y" or Quit == "Y":
print ("Thankyou, Goodbye")
exit()
elif Quit == "No" or Quit == "no" or Quit == "n" or Quit == "N":
print("Okay Great!")
return TriangleFunc()
def TriangleFunc():
print ""
print ("Press Q to quit: ")
Triangle = raw_input("How would you like to draw the triangle? - ASA, AAS, SSS - ")
if Triangle == "ASA" or Triangle == "asa":
ASA()
elif Triangle == "AAS" or Triangle == "aas":
AAS()
elif Triangle == "SSS" or Triangle == "sss":
SSS()
elif Triangle == "Q" or Triangle == "q" or Triangle == "Quit" or Triangle == "quit":
Terminate() # Convert this to a list?
else:
print("That value isn't valid - Please try again: ")
TriangleFunc()
def home():
Answer = raw_input("Would you like to draw a triangle? - (Yes/No) - ")
if Answer == "Yes" or Answer == "yes" or Answer == "Y" or Answer == "y":
TriangleFunc()
else:
End = raw_input("Are you sure you want to quit? ")
if End == "Yes" or End == "yes" or End =="Y" or End == "y":
print ("Thankyou, Goodbye")
exit()
elif End == "No" or End == "no" or End =="N" or End == "n":
return home()
def home2():
print ""
print("------AGAIN?-------")
User = raw_input("Would you like to draw another Triangle? ")
if User == "Yes" or User == "yes" or User == "Y" or User == "y":
TriangleFunc()
elif User == "No" or User == "no" or User =="N" or User == "n":
Terminate()
home()
当用户完成输入第二个三角形的值时,这是我收到的错误:
Traceback (most recent call last):
File "C:\Users\Path to program.py", line 179, in <module>
home()
File "C:\Users\Path to program.py", line 161, in home
TriangleFunc()
File "C:\Users\Path to program.py", line 147, in TriangleFunc
ASA()
File "C:\Users\Path to program.py", line 72, in ASA
sketch(sidea, sideb, sidec, angleA, angleB, angleC)
File "C:\Users\Path to program.py", line 129, in sketch
return home2()
File "C:\Users\Path to program.py", line 175, in home2
TriangleFunc()
File "C:\Users\Path to program.py", line 149, in TriangleFunc
AAS()
File "C:\Users\Path to program.py", line 48, in AAS
sketch(sidea, sideb, sidec, angleA, angleB, angleC)
File "C:\Users\Path to program.py", line 117, in sketch
TD = turtle.Turtle() # TD = Triangle Drawing
File "C:\Python27\lib\lib-tk\turtle.py", line 3707, in __init__
visible=visible)
File "C:\Python27\lib\lib-tk\turtle.py", line 2462, in __init__
self._update()
File "C:\Python27\lib\lib-tk\turtle.py", line 2565, in _update
self._update_data()
File "C:\Python27\lib\lib-tk\turtle.py", line 2551, in _update_data
self.screen._incrementudc()
File "C:\Python27\lib\lib-tk\turtle.py", line 1240, in _incrementudc
raise Terminator
Terminator
对于没有任何潜在解决方案而转储此代码块,我深表歉意,但我真的不知道可能是什么问题。 预先感谢您,Noahf99
问题是您调用了turtle.Screen().exitonclick()
sketch()
。 调用此方法后,您将控制权移交给tkinter,您将无法取回它。 在调用home()
之后,将此调用移动到文件的末尾。 或者,如果您希望在用户第二次确认要退出后关闭窗口,则将其完全关闭。
这个问题与单例无关,因此我会忽略该建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.