omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Help with attribute error

    Pythonista
    5
    6
    3195
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pythonistapro777
      Pythonistapro777 last edited by

      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)
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by 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'
        
        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          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...

          1 Reply Last reply Reply Quote 1
          • ccc
            ccc last edited by ccc

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

            1 Reply Last reply Reply Quote 0
            • Phuket2
              Phuket2 last edited by

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

              1 Reply Last reply Reply Quote 1
              • Webmaster4o
                Webmaster4o last edited by

                @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.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors