【问题标题】:Python-Turtle: Cant interpret through files with both circles and rectangles and draw correctlyPython-Turtle:无法解释带有圆形和矩形的文件并正确绘制
【发布时间】:2016-01-17 04:08:52
【问题描述】:

下面的代码是 3 个函数和一个调用这些函数的 while 循环:

draw_rect():根据给定的参数绘制一个矩形

draw_circle():根据给定的参数画一个圆

draw_line():根据给定的参数画一条线

我的所有功能都可以正常工作,并且可以读取只有矩形、只有线条或只有圆形的文本文件。

我在底部的 while 循环是我遇到问题的地方。我有一个包含矩形和圆形的文件,最终成为 python 徽标。我无法弄清楚这个while循环需要做什么才能同时执行正确的功能。任何帮助将非常感激。我已经放了一个我正在使用的 txt 文件的样本。

蓝色

圆 -30 0 80

圆圈 0 30 80

黄色

圆圈 30 0 80

圆 0 -30 80

蓝色

直角 -84 55 60 110

直角 -84 55 25 120

直角 -30 80 88 82

黑色

圆 -20 -35 38

黄色

圈 -20 -35 32

黑色

直角 -58 -40 5 100

def draw_rect():
    smart = turtle.Turtle()
    i=0
    while i < len(file_list):

        if file_list[i] != "":

                rect = file_list[i]
                if rect[0] != 'rect':
                    return i
                else:
                    color        = str(rect[1])
                    x_coordinate = int(rect[2])
                    y_coordinate = int(rect[3])
                    width        = int(rect[4])
                    height       = int(rect[5])
                    smart.penup()
                    smart.fillcolor(color)
                    smart.begin_fill()
                    smart.goto(x_coordinate, y_coordinate)
                    smart.setheading(0)
                    smart.pendown()
                    smart.forward(width)
                    smart.right(90)
                    smart.forward(height)
                    smart.right(90)
                    smart.forward(width)
                    smart.right(90)
                    smart.forward(height)
                    smart.end_fill()
                    i += 1
    return i

def draw_circle():
    smart = turtle.Turtle()
    i=0
    while i < len(file_list):
        if file_list[i] != "":
                circ = file_list[i]
                if circ[0] != 'circle':
                    return i
                else:
                    color        = str(circ[1])
                    x_coordinate = int(circ[2])
                    y_coordinate = int(circ[3])
                    radius       = int(circ[4])
                    smart.penup()
                    smart.fillcolor(color)
                    smart.begin_fill()
                    smart.goto(x_coordinate, y_coordinate)
                    smart.setheading(0)
                    smart.pendown()
                    smart.circle(radius)
                    smart.end_fill()
                    i += 1
    return i

def draw_line():
    smart = turtle.Turtle()
    i=0
    while i < len(file_list):
        if file_list[i] != "":
            star = file_list[i]
            if start[0] != 'line':
                return i
            else:
                color        = str(star[1])
                x_coordinate = int(star[2])
                y_coordinate = int(star[3])
                angle        = int(star[4])
                line_length  = int(star[5])            
                smart.penup()
                smart.color(color)
                smart.goto(x_coordinate, y_coordinate)
                smart.setheading(angle)
                smart.pendown()
                smart.forward(line_length)
                i += 1
    return i

import turtle
turtle.speed(0)

file = input("What file would you like to execute?")
turtle.clearscreen()
with open(str(file),'r') as f:
    file_list = []
    for line in f:
        if line == '\n': 
            continue

        l = line.split()
        if l[0].lower() == 'color':
            color = l[1].lower()
        else:
            file_list.append([l[0].lower()] + [color] + l[1:])
print(file_list[0:])
n = 0
i = 0
while n < len(file_list):
    n = n + i
    while file_list[n] != "":
            parameter = file_list[n]
            print(parameter[0])
            if parameter[0] != 'line' or parameter[0] != 'circle':
                draw_rect()

            elif parameter[0] != 'line' or parameter[0] != 'rect':
                draw_circle()

            elif parameter[0] != 'circle' or parameter[0] != 'rect':
                draw_line()





print("Program complete")

【问题讨论】:

    标签: python function while-loop turtle-graphics


    【解决方案1】:

    你的逻辑在这里是错误的:

            if parameter[0] != 'line' or parameter[0] != 'circle':
                draw_rect()
    
            elif parameter[0] != 'line' or parameter[0] != 'rect':
                draw_circle()
    
            elif parameter[0] != 'circle' or parameter[0] != 'rect':
                draw_line()
    

    你的意思是and 不是or。假设参数是'circle'。当行

            if parameter[0] != 'line' or parameter[0] != 'circle':
                draw_rect()
    

    被执行,参数不等于'line'所以draw_rect被执行。第二个子句永远不会被评估。

    不过,这是个奇怪的代码。为什么不改成

            if parameter[0] =='rect':
                draw_rect()
    
            elif parameter[0] == 'circle':
                draw_circle()
    
            elif parameter[0] == 'line':
                draw_line()
    

    肯定更清楚了吗?

    编辑:我重新组织了代码,我想你会同意它更容易理解。它肯定更短。最大的变化是

    1) 将绘图参数作为参数传递给绘图函数,而不是将它们保存在全局数组中。 2) 使用迭代器处理文件中的行:for line in file 这样,您不必弄乱索引或担心有多少行。
    3) 请注意我如何将行中的条目转换为整数,例如x, y, width, height = map(int, line[1:]) 有些人不喜欢 map。他们会写类似

    x,y,width,height == [int(item) for item in line[1:]]
    

    随便你的船。

    import turtle
    
    def draw_rect(color, x, y, width, height):
         smart.penup()
         smart.fillcolor(color)
         smart.begin_fill()
         smart.goto(x, y)
         smart.setheading(0)
         smart.pendown()
         smart.forward(width)
         smart.right(90)
         smart.forward(height)
         smart.right(90)
         smart.forward(width)
         smart.right(90)
         smart.forward(height)
         smart.end_fill()
    
    def draw_circle(color, x, y, radius):
         smart.penup()
         smart.fillcolor(color)
         smart.begin_fill()
         smart.goto(x, y)
         smart.setheading(0)
         smart.pendown()
         smart.circle(radius)
         smart.end_fill()
    
    def draw_line(color, x, y, angle, length ):           
         smart.penup()
         smart.color(color)
         smart.goto(x, y)
         smart.setheading(angle)
         smart.pendown()
         smart.forward(line_length)
    
    turtle.speed(0)
    file = input("What file would you like to execute?")
    turtle.clearscreen()
    smart = turtle.Turtle()
    
    with open(str(file),'r')  as f:
         color = 'black'    #default
         for line in f:
              line = line.strip().split()
              if  line[0] == 'color':
                   color = line[1]
              elif line[0] == 'rect':
                   x, y, width, height = map(int, line[1:])
                   draw_rect(color, x, y, width, height)
              elif line[0] == 'circle':
                   x,y, radius = map(int, line[1:])
                   draw_circle(color, x, y, radius)
              elif line[0] == 'line':
                   x, y, angle, length = map(int, line[1:])
                   draw_line(color, x, y, angle, length)
    
    input("Press Enter to exit")               
    print("Program complete")
    

    请注意,其中大部分是您的代码;我刚刚清理了一下。 “我每天都在变好”才是正确的态度!继续努力吧。我的经验以及广大程序员的经验是,您花费在使程序清晰易读的时间可以通过节省调试和后期维护的时间得到充分回报。尽管我为此付出了所有努力,但我无法计算六个月后我查看我的代码并思考“这部分应该做什么?”的次数。

    【讨论】:

    • 谢谢。实际上,我之前就有过这个问题,并且一直遇到另一个问题。自从改回代码后,程序现在可以执行,但只是在评估圆圈时卡住了。
    • 除了while循环中的更改之外,您发布的代码是您当前的代码吗?我会在我的电脑上试试看。
    • 这个问题是由于你在你的while循环中永远不会改变nin 的值始终为零。所以这是一个无限循环。如果你不介意我这么说的话,文件处理代码的结构很糟糕,很难理解。这就是为什么你犯了错误,我想找不到它。给我几分钟,我会发布一些建议的重写。
    • 谢谢,我真的很感激。我只是一个初学者,但每天都在进步。
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多