0

So in Pygame have a function that displays messages to users:

def message_to_user(msg, color, X_displacement, Y_displacement):
    # creating function to display messages to the user
    message=font.render(msg,True,color)
    msgcenter=200+X_displacement,200+Y_displacement
    Game_display.blit(message,msgcenter)

now there is a message I need to display with another variable showing aswell:

        message_to_user("You managed to complete",levelcounter,"levels",green,0,40)

here is the error:

TypeError: message_to_user() takes exactly 4 arguments (6 given)

I understand why I am getting this error, just can't figure a way around it

codeape
  • 97,830
  • 24
  • 159
  • 188
  • 4
    Possible duplicate of [String concatenation in Python](https://stackoverflow.com/questions/3371745/string-concatenation-in-python) – NH. Dec 06 '17 at 18:42
  • The error means what it says. Your function takes four arguments: msg, color, X_displacement, Y_displacement. You've given six. Check out the duplicate link above for how to properly concatenate strings in Python so you only send the four arguments you want. – Rome_Leader Dec 06 '17 at 18:47

4 Answers4

1

You need to build the string before passing it to the function

mess = "You managed to complete {} levels".format(levelcounter)
message_to_user(mess, green, 0, 40)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

I think this ... message_to_user("You managed to complete",levelcounter,"levels",green,0,40)

should become...

message_to_user("You managed to complete" +str(levelcounter)+" levels",green,0,40)

Larry_C
  • 316
  • 1
  • 9
0

You can concatenate the string with the variable to make your message 1 argument

message_to_user("You managed to complete " + levelcounter + " levels",green,0,40)

This would fix the function call to make the arguments match the variables

pastaleg
  • 1,782
  • 2
  • 17
  • 23
0

I would just take the levelcounter as an argument, and then create the msg within the definition.

def message_to_user(levelcounter, color, X_displacement, Y_displacement):
    msg = "You managed to complete " + str(levelcounter) + " levels!"
    message = font.render(msg, True, color)
    msgcenter = 200 + X_displacement, 200 + Y_displacement
    Game_display.blit(message, msgcenter)
Joseph Ryle
  • 129
  • 5