Nov-16-2018, 05:50 AM
(This post was last modified: Nov-16-2018, 05:50 AM by itmustbebunnies.)
Okay, Python gurus - your help solving this issue, please?
I am writing a program that asks a user to make a selection to enter information that is used to create an instance of a class that is then stored in a list to be called upon for drawing (using turtle). I have two different classes: Circle and Rectangle. Here is my main code:
(Also, I know eval is not best practice - but it is how we have been told to do assignments in this class.)
Thank you for your time and effort.
I am writing a program that asks a user to make a selection to enter information that is used to create an instance of a class that is then stored in a list to be called upon for drawing (using turtle). I have two different classes: Circle and Rectangle. Here is my main code:
from circle import Circle from rectangle import Rectangle def main(): playAgain = True list = [] while playAgain: print("1) Enter Circle") print("2) Enter Rectangle") print("3) Remove Shape") print("4) Draw Shapes") print("5) Exit") menu = int(input("What would you like to do? ")) color_list = ["red", "yellow", "blue", "green"] # Enter Circle if menu == 1: centerX, centerY = eval(input("Enter the center position (x,y): ")) radius = eval(input("Enter radius: ")) correct = False while not correct: print("0) Red") print("1) Yellow") print("2) Blue") print("3) Green") color = eval(input("Choose a color: ")) if color == 0 or color == 1 or color == 2 or color == 3: color = color_list[color] correct = True else: print("You have made an invalid selection. Please try again.") correct = False circle = Circle(centerX, centerY, radius, color) list.append(circle) # Enter Rectangle if menu == 2: startX, startY = eval(input("Enter the starting position (x,y): ")) height = eval(input("Enter height: ")) width = eval(input("Enter width: ")) correct = False while not correct: print("0) Red") print("1) Yellow") print("2) Blue") print("3) Green") color = eval(input("Choose a color: ")) if color == 0 or color == 1 or color == 2 or color == 3: color = color_list[color] correct = True else: print("You have made an invalid selection. Please try again.") correct = False rectangle = Rectangle(startX, startY, height, width, color) list.append(rectangle) # Remove Shape if menu == 3: if len(list) > 0: print("The list contains", len(list), "objects.") remove = int(input("Please select an item from 1 to " + str(len(list)) + " to remove that object:")) del list[remove - 1] else: print("The list contains no objects.") # Draw Shapes if menu == 4: for circle in list: circle.draw(centerX, centerY, radius, color) for rectangle in list: rectangle.draw(startX, startY, height, width, color) # Exit if menu == 5: playAgain = False print() print("Thank you for playing.") main()Here is my circle code:
import turtle class Circle: def __init__(self, centerX, centerY, radius, color): self.centerX = centerX self.centerY = centerY self.radius = radius self.color = color def draw(self, centerX, centerY, radius, color): turtle.hideturtle() turtle.penup() turtle.setheading(0) turtle.pencolor(color) turtle.goto(centerX, centerY - radius) turtle.pendown() turtle.fillcolor(color) turtle.begin_fill() turtle.circle(radius) turtle.penup() turtle.end_fill()Here is my rectangle code:
class Rectangle: def __init__(self, startX, startY, height, width, color): self.startX = startX self.startY = startY self.height = height self.width = width self.color = color def draw(self, startX, startY, width, height, color): turtle.hideturtle() turtle.setheading(0) turtle.penup() turtle.goto(startX, startY) turtle.pencolor(color) turtle.fillcolor(color) turtle.pendown() turtle.begin_fill() turtle.forward(width) turtle.right(90) turtle.forward(height) turtle.right(90) turtle.forward(width) turtle.right(90) turtle.forward(height) turtle.end_fill() turtle.penup()When I select to enter a circle, I am able to input all of the info and it appears to store it as an object in the list (when I use
print(list)to verify). The same is true of adding a Rectangle object to the list. The issue I am encountering is when I go to draw the objects. The turtle window will open and the first object will draw, but then I am getting the following error:
Error:Traceback (most recent call last):
File "", line 107, in <module>
main()
File "", line 87, in main
circle.draw(centerX, centerY, radius, color)
TypeError: draw() missing 1 required positional argument: 'color'
The turtle will draw the first shape and fill it in with the color selected for the last object, but then it immediately gives me this error. (Also, I know eval is not best practice - but it is how we have been told to do assignments in this class.)
Thank you for your time and effort.