下面的代码是3个函数和一个用于调用这些函数的while循环:
draw_rect():根据给定的参数绘制矩形
draw_circle():根据给定的参数绘制圆
draw_line():根据给定参数绘制一条直线
我的所有函数都工作正常,可以读取只有矩形、只有线条或只有圆圈的文本文件。
我在底部的while循环是我遇到麻烦的地方。我有一个长方形和圆形的文件,最后变成了一个python徽标。我不知道为了同时执行正确的函数,需要对这个while循环做些什么。任何帮助都将不胜感激。我已经放了一个我正在使用的txt文件的样本。
蓝色
圆圈-30 0 80
圆圈0 30 80
黄色
圆圈30 0 80
圆圈0 -30 80
蓝色
rect -84 55 60 110
rect -84 55 25 120
rect -30 80 88 82
黑色
圆圈-20 -35 38
黄色
圆圈-20 -35 32
黑色
rect -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")发布于 2015-10-19 06:22:00
你的逻辑在这里是错误的:
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()这不是更清楚了吗?
编辑:我重新组织了代码,我想你会同意它更容易理解。当然,shorter.The最大的变化是
1)将绘制参数作为参数传递给绘制函数,而不是将它们保存在全局数组中。2)使用迭代器:for line in file处理文件中的行,这样,您就不必处理索引或担心有多少行。
3)注意我如何将行中的条目转换为整数,例如,一些人不喜欢map的x, y, width, height = map(int, line[1:])。他们会写下这样的话
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")请注意,其中大部分都是您的代码;我只是稍微清理了一下。“我每天都在进步”是正确的态度!坚持下去。我的经验,以及广大程序员的经验是,你花在使你的程序清晰和可读的时间是由你在调试和以后的维护中节省的时间所充分回报的。即使我在这方面付出了所有的努力,我也数不清六个月后我看着我的代码并想,“这一位应该做什么?”
https://stackoverflow.com/questions/33203789
复制相似问题