给定度数和距离的方向,我试图使用函数math.sin和math.cos来计算用户在5x5网格上行进的x和y距离。
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()
答案 0 :(得分:2)
trig函数希望它们的参数位于radians
您可以使用math.radians()
功能将度数转换为弧度
例如:
fire_Direction = math.radians(fire_Direction)
horizontolDirection = round(fire_Distance * (math.cos(fire_Direction)))
verticalDirection = round(fire_Distance * (math.sin(fire_Direction)))
编辑:
我注意到你在这里错误地使用了math.degrees:
fire_Direction=math.degrees(float(input("Please input the angle in degrees of the fire quickly!")))
你应该把它改成math.radians(...)
(这是math.degrees的反面)