0

I am making a Rock Paper Scissors game for a school project. I can't figure out how to make the buttons call the same function with different arguments. Here is my relevant code:

def choose(what):
    Computer = random.randrange(1,3)
    if what == "r":
        if Computer == 2:
            loose
        else:
            win
    elif what == "p":
        if Computer == 3:
            loose
        else:
            win
    elif what == "s":
        if Computer == 1:
            loose
        else:
            win
    else:
        msg.showerror("Invalid Option", "What you choose is invalid. Choose something else.")


Button(root, text="Rock", command=choose("r")).pack()
Button(root, text="Paper", command=choose("p")).pack()
Button(root, text="Scissors", command=choose("s")).pack()

This does not result in an error, but also doesn't call it.

Fighter178
  • 303
  • 2
  • 9
  • Please note that I forgot the imports, so just know that I am using tkinter and random – Fighter178 Oct 03 '21 at 18:32
  • `command` (similar to `after()` and `bind()`) needs so called `callback` which means function's name without `()` and without arguments. If you need to use function with arguments then you can use `lambda` like `command=lambda:choose("r")`. You can also use `functools.partial()`. Or you can create new functions which can run without argument - like `def choose_r(): choose("r")` – furas Oct 03 '21 at 18:34
  • your question is duplicate and links answer your problem. They show how to use `lambda` or `functools.partial` – furas Oct 03 '21 at 18:37
  • I updated the question with a more accurate duplicate. – Karl Knechtel Aug 18 '22 at 03:02

0 Answers0