Python-name';高度';没有定义
我有一个计算机科学作业,我需要编写一个函数Python-name';高度';没有定义,python,global-variables,turtle-graphics,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(
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()
导入海龟
全局startX、startY、宽度、高度、spaceX、spaceY、X位置、Y位置
def drawRectangle():#定义绘制矩形函数
乌龟
乌龟。后藤(xLocation,yLocation)
乌龟
乌龟。左(90)
乌龟,开始填充
乌龟前进(spaceX)
乌龟。左(90)
乌龟。前进(太空)
乌龟。左(90)
乌龟前进(spaceX)
乌龟。左(90)
乌龟。前进(太空)
乌龟
def drawAllRectangles(startX,startY):#将绘制所有矩形以组成棋盘的函数
位置=星形
而在这个函数中,您使用了两次height
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 drawAllRectangles(startX, startY, width, height):
因此,当调用drawAllRectangles时,请确保传入这4个变量
另外,将所有导入内容移动到文件的顶部。并查看eval的功能()。在你的程序中任何地方都不需要globals或eval
当我删除全局变量时,所有的变量都没有定义以供使用,我
获取错误。我能为此做些什么
我同意@anon(+1)关于globals和代码中的eval()。下面是我对sans globals代码的修改——您应该能够根据需要将其分解回两个单独的文件。您需要(重新)阅读global
关键字的作用和使用位置。我知道eval()
输入元组很方便,但你应该找到另一种方法。我将其他eval()
调用替换为int()
:
导入海龟
def drawRectangle(xLocation、yLocation、spaceX、spaceY):
乌龟
乌龟。后藤(xLocation,yLocation)
乌龟
乌龟,开始填充
对于范围(2)内的uu:
乌龟前进(spaceX)
乌龟。左(90)
乌龟。前进(太空)
乌龟。左(90)
乌龟
def DRAWALL矩形(起点、起点、宽度、高度):
“”“绘制所有矩形以组成棋盘”“”
spaceX=宽度/8
spaceY=高度/8
位置=星形
当我删除全局变量时,所有变量都没有定义以供使用,因此会出现错误。我能为此做些什么?
def drawAllRectangles(startX, startY, width, height):
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()