Python-未定义名称“ height”

时间:2018-10-20 00:03:33

标签: python global-variables turtle-graphics

我有一个计算机科学作业,需要编写一个函数main(),该函数调用函数drawChessboard()来绘制8x8棋盘,用户可以在其中确定棋盘的宽度和高度。我们正在学习有关功能和模块的知识,我们需要使用不止一个具有多个功能的模块。这是我拥有的第一个模块:

from chessboard import *

def main():

    startX, startY = eval(input("Enter the starting coordinates (x, y): "))
    width = input("Enter the width: ")
    height = input("Enter the height: ")
    spaceX = eval(width) / 8
    spaceY = eval(height) / 8

    def variables():
        global startX, startY, width, height, spaceX, spaceY, xLocation, yLocation

    if width == "" and height == "":
        drawChessboard(startX, startY)
    elif height == "":
        drawChessboard(startX, startY, width=eval(width))
    elif width == "":
        drawChessboard(startX, startY, height=eval(height))
    else:
        drawChessboard(startX, startY, eval(width), eval(height))


main()

这里是第二个模块,称为:chessboard.py

import turtle

global startX, startY, width, height, spaceX, spaceY, xLocation, yLocation

def drawRectangle():  # Define draw rectangle function

    turtle.penup()
    turtle.goto(xLocation, yLocation)
    turtle.pendown()
    turtle.left(90)
    turtle.begin_fill()
    turtle.forward(spaceX)
    turtle.left(90)
    turtle.forward(spaceY)
    turtle.left(90)
    turtle.forward(spaceX)
    turtle.left(90)
    turtle.forward(spaceY)
    turtle.end_fill()


def drawAllRectangles(startX, startY):          # function that will draw all rectangles to make up chessboard

    yLocation = startY
    while yLocation <= (height - 2 * spaceY):   # draw rectangles on rows 1, 3, 5, 7
        global xLocation
        xLocation = startX
        while xLocation <= (width - 2 * spaceX):
            drawRectangle()
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

    turtle.penup()
    turtle.goto(startX + spaceX, startY + spaceY)
    turtle.pendown()
    yLocation = startY + spaceY
    while yLocation <= (height - spaceY):       # draw rectangles on rows 2, 4, 6, 8
        xLocation = startX + spaceX
        while xLocation <= (width - spaceX):
            drawRectangle()
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

def drawChessboard(startX, startY, width = 250, height = 250):

    import turtle

    turtle.showturtle()         # Draw outside border for chessboard
    turtle.speed(10)
    turtle.penup()
    turtle.goto(startX, startY)
    turtle.pendown()
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(height)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(height)

    drawAllRectangles(startX, startY)

    turtle.done()

当我尝试运行第一个模块时,它给了我while的第一个drawAllRectangles()语句一个错误,提示说 名称height未定义。 我不知道该如何解决。我尝试将所有变量都设置为全局变量,但仍然可以得到相同的结果。我在此网站上查找了错误,但是我没有运气。我在做错什么吗?

2 个答案:

答案 0 :(得分:1)

您在此功能中两次使用了高度

def drawAllRectangles(startX, startY):          # function that will draw all rectangles to make up chessboard

    yLocation = startY
    while yLocation <= (height - 2 * spaceY):   # draw rectangles on rows 1, 3, 5, 7
        global xLocation
        xLocation = startX
        while xLocation <= (width - 2 * spaceX):
            drawRectangle()
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

    turtle.penup()
    turtle.goto(startX + spaceX, startY + spaceY)
    turtle.pendown()
    yLocation = startY + spaceY
    while yLocation <= (height - spaceY):       # draw rectangles on rows 2, 4, 6, 8
        xLocation = startX + spaceX
        while xLocation <= (width - spaceX):
            drawRectangle()
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

该变量在该函数中不存在。不要使用全局。您在

中使用了高度
def drawChessboard(startX, startY, width = 250, height = 250):

所以您需要在这里做同样的事情

def drawAllRectangles(startX, startY, width, height): 

因此,当您调用drawAllRectangles时,请确保传入这4个变量。

此外,将所有导入内容移动到文件顶部。并查看eval的作用(https://www.programiz.com/python-programming/methods/built-in/eval)。您不需要在程序中的任何地方使用globals或eval。

答案 1 :(得分:0)

  

当我删除全局变量时,并未定义所有变量以供使用,   得到错误。我该怎么办?

对于您的代码中的全局变量和eval(),我都同意@anon(+1)。以下是我对无代码全局代码的重做-您应该能够根据需要将其分解为两个单独的文件。您需要(重新)了解global关键字的用途以及使用的位置。我知道eval()在输入元组时很方便,但是您应该找到另一种方法。我用eval()代替了其他int()个电话:

import turtle

def drawRectangle(xLocation, yLocation, spaceX, spaceY):

    turtle.penup()
    turtle.goto(xLocation, yLocation)
    turtle.pendown()

    turtle.begin_fill()

    for _ in range(2):
        turtle.forward(spaceX)
        turtle.left(90)
        turtle.forward(spaceY)
        turtle.left(90)

    turtle.end_fill()

def drawAllRectangles(startX, startY, width, height):
    """ Draw all rectangles to make up chessboard """

    spaceX = width / 8
    spaceY = height / 8

    yLocation = startY

    while yLocation <= (height - 2 * spaceY):  # draw rectangles on rows 1, 3, 5, 7
        xLocation = startX
        while xLocation <= (width - 2 * spaceX):
            drawRectangle(xLocation, yLocation, spaceX, spaceY)
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

    turtle.penup()
    turtle.goto(startX + spaceX, startY + spaceY)
    turtle.pendown()

    yLocation = startY + spaceY

    while yLocation <= (height - spaceY):  # draw rectangles on rows 2, 4, 6, 8
        xLocation = startX + spaceX
        while xLocation <= (width - spaceX):
            drawRectangle(xLocation, yLocation, spaceX, spaceY)
            xLocation += 2 * spaceX
        yLocation += 2 * spaceY

def drawChessboard(startX, startY, width=250, height=250):

    """ Draw outside border for chessboard """

    turtle.penup()
    turtle.goto(startX, startY)
    turtle.pendown()

    for _ in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)

    drawAllRectangles(startX, startY, width, height)

def main():

    startX, startY = eval(input("Enter the starting coordinates (x, y): "))
    width = input("Enter the width: ")
    height = input("Enter the height: ")

    turtle.speed('fastest')

    if width == "" and height == "":
        drawChessboard(startX, startY)
    elif height == "":
        drawChessboard(startX, startY, width=int(width))
    elif width == "":
        drawChessboard(startX, startY, height=int(height))
    else:
        drawChessboard(startX, startY, int(width), int(height))

    turtle.done()

main()

用Python乌龟画棋盘不需要花费太多的代码和精力。我建议您研究stamp()来加快并简化它。