Question

Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):...

Here is a Python program.

import turtle

class Box:

    def __init__(self, x, y, width, height):

        self.x = x

        self.y = y

        self.width = width

        self.height = height

    def show(self):

        turtle.up()

        turtle.goto(x, y)

        turtle.down()

        for _ in range(2):

            turtle.forward(width)

            turtle.right(90)

            turtle.forward(height)

            turtle.right(90)

bigbox = Box(-100, -150, 200, 300)

bigbox.show()

turtle.done()

If you run the above program, it will produce an error. What is the keyword that has been missing from several places in the code inside the show() function?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here in the show function you are using width and height variables which are class variables and to access class variables you have to use self with it, therefore the show function will become :

def show(self):
turtle.up()
turtle.goto(x, y)
turtle.down()
for _ in range(2):
   turtle.forward(self.width)
   turtle.right(90)
   turtle.forward(self.height)
   turtle.right(90)

Add a comment
Know the answer?
Add Answer to:
Here is a Python program. import turtle class Box:     def __init__(self, x, y, width, height):...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Problem 5] List all that the following Python class is missing in order to function. [10...

    [Problem 5] List all that the following Python class is missing in order to function. [10 Points] import math class TwoCoordinatePoints (object): #This class represents a point A = (x, y) with integer values for x and y. -_init__(x, y): self.x = x self.y = y def -_str__(self): "(%, %)" % (self.x, self.y) def distance (self, P): #computes the distance between two points (instances) of this class return math.sqrt((self.x-P.x)**2+(self.y-P.y)**2)

  • fix this python program (error:NameError: name 'turtle' is not defined) def Drawtriangularmotion():     import turtle    ...

    fix this python program (error:NameError: name 'turtle' is not defined) def Drawtriangularmotion():     import turtle     import random index=0 size=[80,75,60] color=['red','green','blue'] myTurtle=turtle.Turtle() screen = turtle.Screen() def draw_square():     global index     myTurtle.fillcolor(color[index])     myTurtle.begin_fill()     for _ in range(4):         myTurtle.left(90)         myTurtle.forward(size[index])     myTurtle.end_fill()     index+=1 def draw_triangle():     for _ in range(3):         draw_square()         myTurtle.penup()         myTurtle.forward(450)         myTurtle.left(120)         myTurtle.pendown() def main():     draw_triangle()     screen.exitonclick() main()

  • In Python: Which keyword should be replaced instead of X? class Class(Car): def __init__ (self,arg1,arg2): X.__init__(arg1,arg2)...

    In Python: Which keyword should be replaced instead of X? class Class(Car): def __init__ (self,arg1,arg2): X.__init__(arg1,arg2) Question 21 options: class def super() super

  • ## python oop creating a large number of instances ## im trying to create a large...

    ## python oop creating a large number of instances ## im trying to create a large number of instances for a class. could you show me a way to make it work import math import random class soldiers: def __init__(self,x,y): self.x = x self.y = y def get_target(self,x,y,number_red_soldiers,number_blue_soldiers): pass # self.target = def aiming_angle(self,x,y,target,enemy_x,enemy_y): dy = self.y-enemy_y[target] dx = self.x-enemy_x[target] angle =math.atan(dy/dx) a = 0 number_blue_soldiers= 10 number_red_soldiers = 10 for i in range(number_blue_soldiers): blue_x = random.randint(0,100,10) blue_y = random.randint(0,100,10)...

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

  • Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...

    Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a triangle at depth k given the bottom vertex.    vertex: a tuple (x,y) that gives the coordinates of the bottom vertex. When k=0, vertex is the left bottom vertex for the outside triangle. length: the length of the original outside triangle (the biggest one). k: the depth of the input triangle. As k increases by 1, the triangles shrink to a smaller size. When k=0, the...

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • 10p Python For the following question, refer to the Python module on the right, as well...

    10p Python For the following question, refer to the Python module on the right, as well as the code below. 1) What is the output of the following code? #dodgeable.py #main dodgeable.py import dodgeable class Locatable: def NUM_CARS = 3 init (self,x,y): NUM TRUCKS = 2 self.mX = X WIDTH = 60 self.mY = y def setx (self,x): def main (): self.mX = x object_list = [] def sety (self, y): self.mY = y for i in range (NUM_CARS) :...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT