Python Forum
NameError: name 'Paddle' is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NameError: name 'Paddle' is not defined (/thread-15531.html)



NameError: name 'Paddle' is not defined - meza1123 - Jan-20-2019

I keep getting this error message.

Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\Anthony\AppData\Local\Programs\Python\Python36-32\paddlebounce.py
Traceback (most recent call last):
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python36-32\paddlebounce.py", line 45, in <module>
paddle = Paddle(canvas, 'blue')
NameError: name 'Paddle' is not defined
>>>


Here is the code:
from tkinter import *
import random
import time
class Ball:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)
        starts = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

    class Paddle:
        def __init__(self, canvas, color):
            self.canvas = canvas
            self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
            self.canvas.move(self.id, 200, 300)

        def draw(self):
            pass
         
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas =Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, 'red')

while 1:
    ball.draw()
    paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
    



RE: NameError: name 'Paddle' is not defined - ichabod801 - Jan-20-2019

Please use python and output tags when posting code and results. The icode tags are for inline code.

Class Paddle is indented under class Ball. Currently it can only be accessed by Ball.Paddle(). You probably just want to unindent that section of code.


RE: NameError: name 'Paddle' is not defined - meza1123 - Jan-22-2019

Thank you so much! Sorry about how I posted it. I'm still learning. I'll make sure to do it right next time.