У меня возникли проблемы с печатью кругов в программе «Эта черепаха». Как мне понять, что говорит мой профессор?Python

Программы на Python
Ответить
Anonymous
 У меня возникли проблемы с печатью кругов в программе «Эта черепаха». Как мне понять, что говорит мой профессор?

Сообщение Anonymous »

Проблема в том, что Circle_locs остается пустым списком.

Измените handle_mouse_click: в зависимости от current_shape добавьте либо к Square_locs, Circle_locs, либо к Path_verts (используйте if-elif-else):
if current_shape == 's':
square_locs.append( [x, y, current_size] )
elif current_shape == 'p':
circle_locs.append( [x, y, current_size] )
else:
(do this one)

Кругов по-прежнему нет! Вам нужно реализовать draw_circles (очень похоже на draw_squares). Также реализуйте draw_circle. Это похоже на draw_square, но вместо цикла for просто вызывается t.circle(radius). Радиус составляет половину текущего_размера.
Теперь будут нарисованы круги и квадраты. У вас проблемы? Добавьте несколько операторов печати в каждую функцию и запустите программу. Посмотрите в консоли IDLE выходные данные отладочной печати.

Я знаю, что мне нужно реализовать этот код, но я не знаю, куда его поместить. Работа, которую я сейчас выполняю, меняет размер квадрата, но мне нужно иметь возможность менять ручку, чтобы теперь иметь возможность делать это с кругами. Это для домашнего задания.
import turtle

"""
This program lets the user draw squares, circles, and polygonal paths.
"""

# The locations of the squares. Each entry is a list, with [x, y, size]
square_locs= []

# The locations of the circles. Each entry is a list, with [x, y, size]
circle_locs = []

# The vertexes of the path. Each entry is a list, with [x, y]

path_verts = []

"""
When the user clicks, either a square, circle, or vertex
gets added. Which one, depends on the current shape:

's' : add a square
'c' : add a circle
'p' : add a vertex to the path
"""
current_shape = 's'

"""
Each added shape has a size. It's whatever the current size
was at the time of the click. This gets changed with the
+ and - keys.
"""
current_size = 15

def draw_square(x, y, size):
"""
Draw a square, at the given x, y position, with the given size
"""
t = turtle
t.up()
t.goto(x, y)
t.down()
for side in range(4):
t.fd(size)
t.left(90)

def draw_circle(x, y, diameter):
"""
Draw a circle at the given x y position, with the given diameter
"""
t = turtle
t.penup()
t.goto(x, y)
t.down()
t.circle()
# ******************************
# You must implement this function.
# ******************************

pass

def draw_squares():
"""
Draw all the existing squares
"""
for point in square_locs:
x, y, size = point
draw_square(x, y, size)

def draw_circles():
"""
Draw all the existing circles
"""
for point in circle_locs:
x, y, size = point
draw_square(x, y, size)

# ******************************
# You must implement this function
# ******************************
pass

def draw_path():
"""
Draw the current path, from its existing vertices
"""
# ******************************
# You must change this function;
# it doesn't quite work.
# ******************************

t = turtle
t.down()
for x, y, size in path_verts:
t.goto(x, y)

def redraw_scene():
"""
Draw all the objects, squares, circles, and path
"""
# Prepare to do fast drawing
turtle.tracer(False)
turtle.hideturtle()
# erase the window
turtle.clear()

# Draw all the object
draw_squares()
draw_circles()
draw_path()

# show the window again
turtle.update()

def handle_mouse_click(x, y):
"""
This is called when the user clicks somewhere
"""
# Add an [x, y, size] list to the list of square locations.
square_locs.append( [x, y, current_size] )
if current_shape == 's':
square_locs.append( [x, y, current_size] )
elif current_shape == 'p':
circle_locs.append( [x, y, current_size] )

# ******************************
# You must modify this function:
# Instead of just adding to square_locs, check current_shape
# and then either add to square_locs, circle_locs, or path_verts
# ******************************

pass

# Don't remove this call to redraw_scene().
# We just added something, so we need to re-draw everything.
redraw_scene()

def handle_key_plus():
"""
This is called when the user presses the + key
"""
# ******************************
# You must change this function
# ******************************
global current_size

current_size += 5

def handle_key_minus():
"""
This is called when the user presses the - key
"""
# ******************************
# You must change this function
# ******************************
global current_size
current_size -= 5
if current_size < 5:
current_size==5
pass

def handle_key_S():
"""
This is called when the user presses the S key
"""
# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 's'

def handle_key_C():
"""
This is called when the user presses the C key
"""

# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 'c'
pass

def handle_key_P():
"""
This is called when the user presses the P key
"""

# ******************************
# You must change this function
# ******************************
global current_shape
current_shape = 'p'
pass

def main():
t = turtle
# Create the turtle drawing window
t.setup(500, 500)

# register callbacks for all the user actions
t.onscreenclick(handle_mouse_click)

t.onkey(handle_key_S, 's')
t.onkey(handle_key_C, 'c')
t.onkey(handle_key_P, 'p')

t.onkey(handle_key_plus, '+')
t.onkey(handle_key_minus, '-')

# get keyboard focus (so that keyboard events will trigger onkey() calls)
t.listen()

# wait for keyboard, mouse, and timer events.
t.mainloop()

if __name__ == '__main__':
# Run the main() function when we run this module,
# but not when we "import" this module.
main()


Подробнее здесь: https://stackoverflow.com/questions/718 ... am-how-can
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»