Question

Given a direction in degrees and a distance, I am attempting to use the functions math.sin and math.cos to calculate the x and y distance a user would have to travel over a 5x5 grid.

A) Can someone tell me why my code is experiencing logic errors? It all checks out to me.

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!

It returns the wrong direction to go (south instead of north), and it instructs the user to go west 1 block. This is dreadful.

My second question is this: How would I make it so the turtle would draw a blue line along the grid from the center point to whichever block was closest to the grid coordinates the user input.

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()
Était-ce utile?

La solution

The trig functions expect their arguments to be in radians

You can use the math.radians() function to convert degrees to radians

eg:

fire_Direction = math.radians(fire_Direction)
horizontolDirection = round(fire_Distance * (math.cos(fire_Direction)))
verticalDirection = round(fire_Distance * (math.sin(fire_Direction)))

EDIT:

I noticed you are using math.degrees incorrectly here:

fire_Direction=math.degrees(float(input("Please input the angle in degrees of the fire quickly!")))

You should probably just change that one to math.radians(...) (which is the inverse of math.degrees)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top