龟在运行期间意外崩溃

时间:2014-01-30 00:41:57

标签: python

我添加了一段新的代码,现在当我运行我的程序龟图形崩溃时。我没有在空闲时收到运行时错误,所以我不知道要修改什么来修复它

当我实现gotoFire()函数时,崩溃才刚刚开始。我无法删除它,因为它对该程序的操作至关重要!

import turtle
import math
NUMBER_OF_LICENSES=10

def goodbyeMessage():
    print("Thank you for using the program!")

def getInfo():
    fire_Direction=math.radians(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):

    sideOfFireHouseToLeaveFrom=[]

    if horizontolDirection < 0:
        print("The fire is %d streets to the west of the firehouse!" %horizontolDirection)
        sideOfFireHouseToLeaveFrom += 'west'
    elif horizontolDirection > 0:
        print("The fire is %d streets to the east of the firehouse!" %horizontolDirection)
        sideOfFireHouseToLeaveFrom += 'east'
    else:
        sideOfFireHouseToLeaveFrom += 'void'

    if verticalDirection < 0:
        print("The fire is %d streets to the south of the firehouse!" %verticalDirection)
        sideOfFireHouseToLeaveFrom += 'south'
    elif verticalDirection > 0:
        print("The fire is %d streets to the north of the firehouse!" %verticalDirection)
        sideOfFireHouseToLeaveFrom += 'north'
    else:
        pass

    if verticalDirection == 0 and horizontolDirection == 0:
        print("The firehouse is on fire!")
        sideOfFireHouseToLeaveFrom += 'void' + 'void'

    return sideOfFireHouseToLeaveFrom

def gotoFire(horizontolDirection,verticalDirection,sideOfFireHouseToLeaveFrom):



    if sideOfFireHouseToLeaveFrom[0] == "east":
        turtle.forward(300)
        turtle.right(90)


    elif sideOfFireHouseToLeaveFrom[0] == "west":
        turtle.forward(200)
        turtle.right(90)

    else:
        pass

    if sideOfFireHouseToLeaveFrom[1] == "north":
        turtle.forward(200)

    elif sideOfFireHouseToLeaveFrom[1] == "south":
        turtle.forward(300)

    else:
        pass





def giveDirection(fire_Direction,fire_Distance):
    horizontolDirection = int(abs(round(fire_Distance * (math.cos(fire_Direction)))))
    verticalDirection = int(abs(round(fire_Distance * (math.sin(fire_Direction)))))
    sideOfFireHouseToLeaveFrom = announceDirections(horizontolDirection,verticalDirection)

    gotoFire(horizontolDirection,verticalDirection,sideOfFireHouseToLeaveFrom)



    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()

1 个答案:

答案 0 :(得分:0)

您将字符列表传递给sideOfFireHouseToLeaveFrom

gotoFire参数

例如:

['v', 'o', 'i', 'd', 'n', 'o', 'r', 't', 'h']

这似乎不正确。我不认为你的程序崩溃了。它只是没有做任何事情,因为if条件都是False

回顾过去,我们发现坏数据似乎来自announceDirections。因为您正在尝试将字符串添加到列表中。

>>> sideOfFireHouseToLeaveFrom=[]
>>> sideOfFireHouseToLeaveFrom += 'west'
>>> sideOfFireHouseToLeaveFrom
['w', 'e', 's', 't']

您可能应该使用sideOfFireHouseToLeaveFrom.append('west')

>>> sideOfFireHouseToLeaveFrom=[]
>>> sideOfFireHouseToLeaveFrom.append('west')
>>> sideOfFireHouseToLeaveFrom
['west']