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.


    [SOLVED!] Help with global error

    Pythonista
    2
    2
    1675
    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 Pythonistapro777

      Making a relatively simple rock, paper scissors game. Please help with the global error that pops up when the code is run.

      Thanks in advance!

      Here's the code:

      #coding: utf-8
      choices = ["✂️", "✋", "🌚"]
      
      from scene import *
      import random
      
      class rps(Scene):
      	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.add_clicks
      		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.add_clicks1
      		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.add_clicks2
      		
      	def draw(self):
      		background(0,0.05,0.2)
      		self.button.draw()
      		self.button1.draw()
      		self.button2.draw()
      		
      	def add_clicks(sender):
      		global player
      		player = "🌚"
      		global computer
      		computer = random.choice(choices)
      		
      	def add_clicks1(sender):
      		global player
      		player = "📄"
      		global computer
      		computer = random.choice(choices)
      		
      	def add_clicks2(sender):
      		global player
      		player = "✂️"
      		global computer
      		computer = random.choice(choices)
      		
      run(rps())
      print(player)
      print(computer)
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by ccc

        • Remove all lines that start with the word global.
        • Rename all instances of player to self.player.
        • Rename all instances of computer to self.computer.
        • self.add_layer() each of the three buttons to enable their actions.
        • Create a .stop() method to wait until the user closes the scene.Scene.
        • etc.

        See: https://github.com/cclauss/Magic8Ball/blob/patch-4/rock_paper_scissors.py

        Try to avoid global variables where ever possible.

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