6

I have been programming from the book Python for Absolute Beginners, using Python 3.3.1.

I am trying to add text to a screen using the following code. I need to stay in Python 3.3.1 but the code from the book I think is for Python 2.X.

from livewires import games, color

class Pizza(games.Sprite):
    """A falling pizza"""
    def __init__(self, screen, x,y, image, dx, dy):
        """Initialise pizza object"""
        self.init_sprite(screen = screen, x = x, y = y, image = image, dx = dx, dy = dy)

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

#main

my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)

wall_image = games.load_image("wall.jpg", transparent = False)
pizza_image = games.load_image("pizza.jpg")
my_screen.set_background(wall_image)
games.Text(screen = my_screen, x = 500, y = 30, text = "Score: 1756521", size = 50, color = 

my_screen.mainloop()

However, when I run this program I get an error (see below)

  games.Text(screen = my_screen, x = 500, y = 30, text = "Score: 1756521", size = 50, color = color.black)
TypeError: __init__() got an unexpected keyword argument 'text'

I hope you can help

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3375363
  • 61
  • 1
  • 1
  • 2
  • 4
    games.Text() is creating an object, but the __init__() inside the class which defines this object does not expect a keyword argument called text, hence the exception. I suggest you have a look at the __init__() inside the games.Text class – dannymilsom Mar 03 '14 at 15:17

1 Answers1

1

I replied with a comment, but I thought I would elaborate with a full answer.

I've just looked at the source code for the livewires games.py module as I suggested

class Text(Object, ColourMixin):
    """
    A class for representing text on the screen.

    The reference point of a Text object is the centre of its bounding box.
    """

    def __init__(self, screen, x, y, text, size, colour, static=0):
        self.init_text (screen, x, y, text, size, colour, static)
        .........

So as you can see, the __init__() for the Text class does not expect a keyword argument called text. Instead it expects a series of positional arguments.

So your code should look like this

games.Text(my_screen, 500, 30, "Score: 1756521", 50, color.black)

Edit:

As noted by 2rs2ts, you can provider keywords arguments to positional arguments if you specify all the argument names. However as you used the keyword color, not colour, your code failed. So the following should also work (but I would recommend the use positional arguments)

games.Text(screen=my_screen, x=500, y=30, text="Score: 1756521", size=50, colour=color.black)

According to PEP8 you should also note - "Don't use spaces around the = sign when used to indicate a keyword argument or a default parameter value."

dannymilsom
  • 2,386
  • 1
  • 18
  • 19
  • 1
    But you can provide keywords for the positional arguments. Try it yourself in your interpreter. `def foo(a,b,c): return a+b+c` then `foo(a=1, b=2, c=3)`. – 2rs2ts Mar 03 '14 at 16:46
  • Yes that is true. His code actually failed because he misspelled colour as color, and therefore using keywords for positional arguments failed. So he could either change color to colour, or use positional args. I'll update my answer based on this – dannymilsom Mar 03 '14 at 16:52
  • I was going to add the note about the misspelling but then I figured that it shouldn't have complained about `text` when the problem was `color`... – 2rs2ts Mar 03 '14 at 18:56