Forum Archive

[SOLVED!] Help with global error

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