from Tkinter import *
import random

weapons = ("rock", "paper", "scissors")

class Application(Frame):
    
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):

        Label(self,
              text = "Choose your weapon!\n"
              ).grid(row = 0, column = 0, sticky = W)

        Label(self,
              text = "Weapons:"
              ).grid(row = 1, column = 0, sticky = W)

        self.weapon = StringVar()

        column = 1
    
        Radiobutton(self,
                    text = "Rock",
                    variable = self.weapon,
                    value = "rock"
                    ).grid(row = 1, column = column, sticky = W)

        Radiobutton(self,
                    text = "Paper",
                    variable = self.weapon,
                    value = "paper"
                    ).grid(row = 2, column =+ 1, sticky = W)

        Radiobutton(self,
                    text = "Scissors",
                    variable = self.weapon,
                    value = "scissors"
                    ).grid(row = 3, column =+ 1, sticky = W)

        Button(self,
               text = "Battle!",
               command = self.fight
               ).grid(row = 3, column = 0, columnspan = 4, sticky = W)
               

        self.outcome_txt = Text(self, width = 50, height = 2, wrap = WORD)
        self.outcome_txt.grid(row = 7, column = 0, columnspan = 4)

    def comp_choice(self):
        comp_throw = random.randrange(len(weapons))
        opponent = comp_throw

    def fight(self):
        weapon = self.weapon.get()
        op = self.comp_choice
        print weapon
        print str(self.comp_choice)
        if weapon == op:
            outcome = "You tied, play again!"
        elif weapon == "scissors" and op == "paper" or \
             weapon == "paper" and op == "rock" or \
             weapon == "rock" and op == "scissors":
            outcome = "The computer threw: " + op + " - You won!"
        elif weapon == "scissors" and op == "rock" or \
             weapon == "paper" and op == "scissors" or \
             weapon == "rock" and op == "paper":
            outcome = "The computer threw: " + op + "- You lose! :("
        else: outcome = "You didn't throw anything1"

        self.outcome_txt.delete(0.0, END)
        self.outcome_txt.insert(0.0, outcome)

root = Tk()
root.title("Rock, Paper, Scissors")
app = Application(root)
root.mainloop()

Right now when it runs, it recognizes that the user selected either rock, paper, or scissors, but when it comes time to do the fight function, it says that you didn't throw anything. It's clear to me that it's not able to compare the 'value' variable with the computer's choice.

Any ideas?

Recommended Answers

All 3 Replies

Minor fixes are commented.

from Tkinter import *
import random

weapons = ("rock", "paper", "scissors")

class Application(Frame):
    
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):

        Label(self,
              text = "Choose your weapon!\n"
              ).grid(row = 0, column = 0, sticky = W)

        Label(self,
              text = "Weapons:"
              ).grid(row = 1, column = 0, sticky = W)

        self.weapon = StringVar()

        column = 1
    
        Radiobutton(self,
                    text = "Rock",
                    variable = self.weapon,
                    value = "rock"
                    ).grid(row = 1, column = column, sticky = W)

        Radiobutton(self,
                    text = "Paper",
                    variable = self.weapon,
                    value = "paper"
                    ).grid(row = 2, column =+ 1, sticky = W)

        Radiobutton(self,
                    text = "Scissors",
                    variable = self.weapon,
                    value = "scissors"
                    ).grid(row = 3, column =+ 1, sticky = W)

        Button(self,
               text = "Battle!",
               command = self.fight
               ).grid(row = 3, column = 0, columnspan = 4, sticky = W)
               

        self.outcome_txt = Text(self, width = 50, height = 2, wrap = WORD)
        self.outcome_txt.grid(row = 7, column = 0, columnspan = 4)

    def comp_choice(self):
        comp_throw = random.randrange(len(weapons))
        opponent = weapons[comp_throw]  #index for the random #
        return opponent #need to return opponent

    def fight(self):
        weapon = self.weapon.get()
        op = self.comp_choice() #forgot ()
        if weapon == op:
            outcome = "You tied, play again!"
        elif weapon == "scissors" and op == "paper" or \
             weapon == "paper" and op == "rock" or \
             weapon == "rock" and op == "scissors":
            outcome = "The computer threw: " + op + " - You won!"
        elif weapon == "scissors" and op == "rock" or \
             weapon == "paper" and op == "scissors" or \
             weapon == "rock" and op == "paper":
            outcome = "The computer threw: " + op + "- You lose! :("
        else: outcome = "You didn't throw anything1"

        self.outcome_txt.delete(0.0, END)
        self.outcome_txt.insert(0.0, outcome)

root = Tk()
root.title("Rock, Paper, Scissors")
app = Application(root)
root.mainloop()

In my opinion, this is a better way to write fight():

def fight(self):
        weapon = self.weapon.get()
        op = self.comp_choice()
        defeats = {"scissors":"paper",  #scissors defeats paper
                   "paper":"rock",  #paper defeats rock
                   "rock":"scissors"}  #rock defeats scissors
        if not weapon:  #if the user didn't select a radiobutton
            outcome = "Please choose a weapon!"
        elif weapon == op:
            outcome = "You tied!"
        elif defeats[weapon] == op:
            outcome = "The computer threw: %s - You won!" % op
        else:
            outcome = "The computer threw: %s - You lost!" % op

        self.outcome_txt.delete(0.0, END)
        self.outcome_txt.insert(0.0, outcome)
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.