Forum Archive

Help with attribute error

Pythonistapro777

Please help with attribute error that pops up when the code is run.

Thanks in advance!

Here's the code:

# coding: utf-8

from scene import *
import random

choices = '🌚', '📄', '✂️'

class rps(Scene):
    def __init__(self):
        self.player = self.computer = None

    def setup(self):
        self.button = Button(Rect(self.size.w/2-60, self.size.h/1-150, 125, 125))
        self.button.background = Color(0,0,0,0)
        self.button.stroke = Color(0,0,0,0)
        self.button.image = 'Moon_2'
        self.button.action = self.rock_action
        self.add_layer(self.button)

        self.button1 = Button(Rect(self.size.w/2-60, self.size.h/1-290, 125, 125))
        self.button1.background = Color(0,0,0,0)
        self.button1.stroke = Color(0,0,0,0)
        self.button1.image = 'Page_Facing_Up'
        self.button1.action = self.paper_action
        self.add_layer(self.button1)

        self.button2 = Button(Rect(self.size.w/2-60, self.size.h/1-450, 125, 125))
        self.button2.background = Color(0,0,0,0)
        self.button2.stroke = Color(0,0,0,0)
        self.button2.image = 'Scissors'
        self.button2.action = self.scissors_action
        self.add_layer(self.button2)

    def rock_action(self):
        self.button.background = Color(0,0,0,0)
        self.player = '🌚'
        self.computer = random.choice(choices)

    def paper_action(self):
        self.button1.background = Color(0,0,0,0)
        self.player = '📄'
        self.computer = random.choice(choices)

    def scissors_action(self):
        self.button2.background = Color(0,0,0,0)
        self.player = '✂️'
        self.computer = random.choice(choices)

    def draw(self):
        background(0, 0.05, 0.2)
        self.root_layer.update(self.dt)
        self.root_layer.draw()

        if self.player == "🌚" and self.computer == "🌚":
            self.ruling = "Draw"

        if self.player == "✂️" and self.computer == "✂️":
            self.ruling = "Draw"

        if self.player == "📄" and self.computer == "📄":
            self.ruling = "Draw"

        if self.player == "🌚" and self.computer == "✂️":
            self.ruling = "Win"

        if self.player == "🌚" and self.computer == "📄":
            self.ruling = "Lose"

        if self.player == "📄" and self.computer == "🌚":
            self.ruling = "Win"

        if self.player == "📄" and self.computer == "✂️":
            self.ruling = "Lose"

        if self.player == "✂️" and self.computer == "🌚":
            self.ruling = "Lose"

        if self.player == "✂️" and self.computer == "📄":
            self.ruling = "Win"

        print(self.ruling)

run(rps(), PORTRAIT)
ccc
def get_ruling(player_choice, opponent_choice):
    if player_choice == opponent_choice:
        return "Draw"
    elif player_choice == '🌚':
        return 'Win' if opponent_choice == '✂️' else 'Lose'
    elif player_choice == '📄':
        return 'Win' if opponent_choice == '🌚' else 'Lose'
    else: # player_choice == '✂️'
        return 'Win' if opponent_choice == '📄' else 'Lose'
JonB

it would be helpful when posting request to debug your code, if you also post the traceback! that way, you don't force ccc to copy/paste/run your code in order to help...

ccc

In __init__(), add the line self.ruling = None

Phuket2

In your init method, add self.ruling = None
It's basically just not defined

Webmaster4o

@Pythonistapro777 I think you need to start making more of an effort to debug your own code before you post here. If you post every problem you run into with no clear effort to debug made, it's kind of annoying to some users. Try googling for answers similar to yours. For example, search for "Attribute error Python" to find out more about what your error means and work from there.