Проблема в том, что 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
У меня возникли проблемы с печатью кругов в программе «Эта черепаха». Как мне понять, что говорит мой профессор? ⇐ Python
Программы на Python
1766895126
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()
Подробнее здесь: [url]https://stackoverflow.com/questions/71890790/i-am-having-trouble-getting-circles-to-print-in-my-this-turtle-program-how-can[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия