我目前正在编写一个三角形计算器,它根据用户的输入解算三角形,并使用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
答案 0 :(得分:0)
问题是您致电turtle.Screen().exitonclick()
中的sketch()
。一旦你调用这个方法,你就可以将控制权转交给tkinter,你就无法取回它。在致电home()
后,将此来电移至档案的最后。如果您希望在用户第二次确认他们想要退出后关闭您的窗口,请完全取消。
这个问题与单身人士无关,所以我忽略了这个建议。