Ok folks...more help with a programming class...

I'm seriously starting to think programming is not for me, but I just want to finish out this class with a decent grade. I'm not looking for answers...just some guidance in the right direction.

Here is the problem...

As an exercise, write a function named moveRect that takes a
Rectangle and two parameters named dx and dy. It should change
the location of the rectangle by adding dx to the x coordinate of corner
and adding dy to the y coordinate of corner.

Here is what I have so far...

class Rectangle:
    pass
class Point:
    pass

box = Rectangle()
box.width = 100
box.height = 200

box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0

def findCenter(box):
    p = Point()
    p.x = box.corner.x + box.width/2.0
    p.y = box.corner.y - box.height/2.0
    return p

def printPoint(p):
    print "(" + str(p.x) + ", " + str(p.y) + ")"

center = findCenter(box)
printPoint(center)

def moveRect(box, dx, dy) :
    box.corner = Point()
    box.corner.x = box.corner.x + dx
    box.corner.y = box.corner.y + dy

When I run it and input variables for dx and dy, it gives me this error:

>>> moveRect(box, 10, 20)

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    moveRect(box, 10, 20)
  File "C:/Users/Tony/Documents/Module 4 CS/moveRect", line 28, in moveRect
    box.corner.x = box.corner.x + dx
AttributeError: Point instance has no attribute 'x'

Thanks for any help.

The first thing you have to do is decide which GUI toolkit you wish to code for as you will have to move, or destroy/remove the original rectangle graphic and draw/display it in a different location depending on the toolkit. A pretty standard example using Tkinter:

import Tkinter as tk
import time
 
root = tk.Tk()
 
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
 
# canvas.create_rectangle(x0, y0, x1, y1, option, ... )
# x0, y0, x1, y1 are corner coordinates of ulc to lrc diagonal
rc1 = canvas.create_rectangle(20, 260, 120, 360, outline='white', fill='blue')
rc2 = canvas.create_rectangle(20, 10, 120, 110, outline='white', fill='red')
 
for j in range(50):
    y = x = 5
    time.sleep(0.05)
    canvas.move(rc1, x, -y)
    canvas.move(rc2, x, y)
    canvas.update()
 
root.mainloop()

try with:

try:
	# what you want ...
except AttributeError:
	print "The Error message"
else:
	break

is this tk?

We don't know which GUI toolkit is being used and the OP hasn't responded for 6 days, so this thread is dead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.