Python Forum
another positional argument error (...and executing objects stored in a list)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
another positional argument error (...and executing objects stored in a list)
#1
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:

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.
Reply


Messages In This Thread
another positional argument error (...and executing objects stored in a list) - by itmustbebunnies - Nov-16-2018, 05:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,370 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing 1 required positional argument in python code edwinostby 7 10,156 Jan-19-2021, 12:52 PM
Last Post: Serafim
  Missing positional arguments error?? hhydration 2 2,199 Oct-01-2020, 05:33 AM
Last Post: buran
  List of Objects print <__main. Problem Kol789 10 3,630 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  missing positional argument error programmert 1 2,864 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument jedmond2 4 6,840 Sep-19-2019, 12:00 PM
Last Post: jefsummers
  missing 1 required positional argument mcgrim 10 19,975 May-07-2019, 09:02 PM
Last Post: Yoriz
  sorting a deck of cards (objects in a list) itmustbebunnies 1 7,287 Dec-05-2018, 02:44 AM
Last Post: ichabod801
  TypeError: method missing 1 positional argument koolinka 4 5,135 Nov-18-2018, 04:53 PM
Last Post: ichabod801
  Class positional argument error itmustbebunnies 2 3,050 Nov-07-2018, 11:09 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020