給定一個方向的度數和距離,我試圖使用函數math.sin和math.cos來計算用戶將不得不在5x5網格上行進的x和y距離。如何正確使用trig函數?
A)有人能告訴我爲什麼我的代碼遇到邏輯錯誤嗎?這一切都檢查出來給我。
Please input the angle in degrees of the fire quickly!90
Please input the distance of the fire ~VERY~ quickly (input a number between 1 and 5)5
The fire is 1.000000 streets to the west of the firehouse!
The fire is 5 streets to the south of the firehouse!
它返回錯誤的方向去(南而不是北),它指示用戶去西1塊。這是可怕的。
我的第二個問題是這樣的: 我該如何做到這一點,所以烏龜會沿着網格從中心點向最接近網格座標的用戶輸入繪製藍線。
CODE:
import turtle
import math
NUMBER_OF_LICENSES=10
def goodbyeMessage():
print("Thank you for using the program!")
def getInfo():
fire_Direction=math.degrees(float(input("Please input the angle in degrees of the fire quickly!")))
fire_Distance=float(input("Please input the distance of the fire ~VERY~ quickly (input a number between 1 and 5)"))
return fire_Direction,fire_Distance
def announceDirections(horizontolDirection,verticalDirection):
if horizontolDirection < 0:
print("The fire is %f streets to the west of the firehouse!" %abs(int(horizontolDirection)))
elif horizontolDirection > 0:
print("The fire is %f streets to the east of the firehouse!" %abs(int(horizontolDirection)))
else:
pass
if verticalDirection < 0:
print("The fire is %d streets to the south of the firehouse!" %abs(int(verticalDirection)))
elif verticalDirection > 0:
print("The fire is %d streets to the north of the firehouse!" %abs(int(verticalDirection)))
else:
pass
if verticalDirection == 0 and horizontolDirection == 0:
print("The firehouse is on fire!")
def giveDirection(fire_Direction,fire_Distance):
horizontolDirection = round(fire_Distance * (math.cos(fire_Direction)))
verticalDirection = round(fire_Distance * (math.sin(fire_Direction)))
announceDirections(horizontolDirection,verticalDirection)
return horizontolDirection, verticalDirection
def reportFire():
fire_Direction,fire_Distance=getInfo()
horizontolDirection,verticalDirection = giveDirection(fire_Direction,fire_Distance)
def drawHorizontal():
turtle.speed(0)
turtle.penup()
turtle.forward(-300)
turtle.left(90)
turtle.forward(300)
turtle.right(90)
turtle.pendown()
for i in range(5):
turtle.forward(500)
turtle.forward(-500)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(500)
turtle.left(90)
turtle.forward(500)
turtle.left(90)
turtle.forward(500)
turtle.left(180)
def drawVertical():
for i in range(5):
turtle.forward(100)
turtle.right(90)
turtle.forward(500)
turtle.forward(-500)
turtle.left(90)
turtle.forward(-500) #Back to upper left corner which will be main drawing control point
def drawFireStation():
#From main drawing control point
turtle.penup()
#225 instead of 250 so firestation circle is centered in middle of grid
turtle.forward(225)
turtle.right(90)
turtle.forward(250)
turtle.pendown()
turtle.circle(25)
turtle.penup()
turtle.forward(-250)
turtle.left(90)
turtle.forward(-225)
def drawGrid():
turtle.showturtle()
turtle.speed(0)
drawHorizontal()
drawVertical()
drawFireStation()
def main():
drawGrid()
for i in range(NUMBER_OF_LICENSES):
reportFire()
goodbyeMessage()
if __name__ == "__main__":
main()
它拼寫爲「水平」。 –