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?
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)

Here is a Python program. import turtle class Box: def __init__(self, x, y, width, height):...
[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 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) Question 21 options: class def super() super
Please write a code in python! Define and test a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function, but it uses the given RGB values instead of black. images.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight())...
Please convert the following python program to LINE BY LINE python pseudocode. Please convert LINE BY LINE (typed) class vector: def __init__(self,x,y): self.x = x # Assigning x to vector's x self.y = y # Assigning y to vector's y def __str__(self): return '('+str(self.x)+","+str(self.y)+')' # This returns the value of vector in bracket format def __add__(self,other): x = self.x+other.x # values of x of vector1 and vector2 are added forming new x value y = self.y+other.y # same is done...
#python oop referencing attributes in objects # problem is the angle function in my class. import random class people(): def __init__(self,x,y,size_of_other_team): self.x=x self.y=y self.target = random.randint(0,size_of_other_team) # choose a random guy from other team def angle(self, other_teams_x, other_teams_y): # get angle between this guy and his target dx = other_teams_x[self.target]-self.x dy = other_teams_y[self.target]-self.y theta = math.atan(dy/dx) return(theta) def move(self): self.x+=1 red_guys = 10 blue_guys = 10 red_x=[] red_y=[] red=[] # objects list for i in range(red_guys): red_x.append(random.randint(0,100)) red_y.append(random.randint(0,100)) red_person =...
## 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, 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()
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 = 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...