У меня есть следующий код, который создает класс формы, и у меня есть два вопроса, на которые я был бы признателен за ответы: 1. При запуске следующего кода результат будет таким:
>>>
100
100
None
>>>
Что такое "Нет" в конце и как избавиться от этого вывода?
<сильный>2. В идеале я хотел бы иметь возможность РИСОВАТЬ (на экране вывода) квадрат. Я не хочу использовать pygame. Я задавался вопросом, можно ли интегрировать черепаху для этого, но не знаю, с чего начать? Любые предложения по методу, который сделает это с помощью черепахи, или любое другое гениальное предложение?
from turtle import*
class Shape:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self):
print(self.x)
print(self.y)
square=Shape(100,100)
print(square.print())
Я мог бы добавить, что на SO есть аналогичный вопрос, но нет конкретного или полезного ответа.
Использование класса для рисования фигур в черепахе
ОБНОВИТЬ:
Я пробовал что-то вроде этого, но не могу заставить его работать. Я думаю, что мне нужно инициализировать черепаху где-то в конструкторе, но где и как
from turtle import*
class Shape:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self,shapename):
print("This shape is a", shapename, "with dimensions:>",self.x,"by",self.y)
def draw(self):
turtle.forward(self.x)
turtle.left(90)
turtle.forward(se.f.x)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
square=Shape(100,100)
square.print("square")
print("The perimeter is:",square.perimeter())
print(square.draw())
print(square.print())
Эта строка генерирует None на консоли, так как функция ничего не возвращает. Чтобы удалить его, просто удалите внешний print() и оставьте его. то естьsquare.print()
- person Mahesh Karia   schedule 04.11.2017from turtle import *
иclass Shape:
, вы переопределяете собственный классShape
черепахи! Вы можете подтвердить это, напечатавid(Shape)
до и после вашегоclass Shape:
(пере)определения. Вы должны либо ограничить свой импорт,import turtle
илиfrom turtle import Turtle, Screen
, либо переименовать свой классShape
во что-то другое. - person cdlane   schedule 09.11.2017