Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boolean Wont Work
#1
Under variables I set up a boolean and later in Mknight() I set it to true then set up an if in Landscape() to check if it was true and if it was to draw it but it Doesnt. PLEASE HELP btw I dont get any errors
from graphics import*
from pygame import*
import pygame
import graphics

##Variables##
x,y=0,0     #positions of mouse clicks
p1 = GraphWin("coord locator",1440,900)
Morange=False
Mpurple=False

##Main##
p1.setBackground('blue')
txt = Text(Point(725,125),'Select Your class')
txt.setTextColor('red')
txt.setSize(16)
txt.setFace('times roman')
txt.draw(p1)
txt1 = Text(Point(690,200),'Mounted Knight\t\t\t\t\t Knight')
txt1.setTextColor('red')
txt1.setSize(16)
txt1.setFace('times roman')
txt1.draw(p1)
class1=Image(Point(450,400),'Mknightlogo.gif')
class1.draw(p1)
class2=Image(Point(975,400),'Knightlogo.gif')
class2.draw(p1)
Mknighto=Image(Point(200,600),'Mknighto.gif')
Eknight=Image(Point(1200,700),'Eknight.gif')
Mknightp=Image(Point(200,600),'Mknightp.gif')

##Functions##

def Landscape():
    grass = Rectangle(Point(0,765),Point(1450,900))
    grass.setFill('green')
    grass.draw(p1)
    sky = Rectangle(Point(0,765),Point(1450,0))
    sky.setFill('sky blue')
    sky.draw(p1)
    sun=Image(Point(1200,200),'sun.gif')
    Eknight.draw(p1)
    while True:
        if Morange == True:
            Mknighto.draw(p1)
            print ('this is working sort of')
        if Mpurple == True:
            Mknightp.draw(p1)
            print ('this is working sort of')

def Mknight():
    p1.setBackground('green')
    txt.setText('You haven choosen your trusty study, choose your Colour')
    txt.draw(p1)
    txt1.setText('Orange\t\t\t\t\t Purple')
    txt1.draw(p1)
    txt1.move(40,0)
    clr=Image(Point(490,450),'orangecl.gif')
    clr.draw(p1)
    clr1=Image(Point(975,450),'purplecl.gif')
    clr1.draw(p1)
    while True:
        p=p1.checkMouse()  
        if p:
            x=p.getX()
            y=p.getY()
            print(x,y)
            if 290<x<690 and 250<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                Morange = True
                Landscape()
                break
            if 775<x<1175 and 250<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                Mpurple = True
                Landscape()



def Knight():
    p1.setBackground('green')
    txt.setText('You haven choosen your trusty study, choose your Colour')
    txt.draw(p1)
    txt1.setText('Orange\t\t\t\t\t Purple')
    txt1.draw(p1)
    txt1.move(40,0)
    clr=Image(Point(490,450),'orangecl.gif')
    clr.draw(p1)
    clr1=Image(Point(975,450),'purplecl.gif')
    clr1.draw(p1)
    while True:
        p=p1.checkMouse()
        p1.setBackground('green')  
        if p:
            x=p.getX()
            y=p.getY()
            print(x,y)
            if 290<x<690 and 250<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                Knightp=Image(Point(200,600),'Knightp.gif')
                Knightp.draw(p1)
                Eknight=Image(Point(100,700),'Eknight.gif')
                Eknight.draw(p1)
                Landscape()
                break
            if 400<x<690 and 500<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                Knighto=Image(Point(200,600),'Knighto.gif')
                Knighto.draw(p1)
                Eknight=Image(Point(1200,700),'Eknight.gif')
                Eknight.draw(p1)
                Landscape()
                break


while True:
    p=p1.checkMouse()
    p1.setBackground('blue')  
    if p:
        x=p.getX()
        y=p.getY()
        print(x,y)
        if 300<x<595 and 230<y<560 :
            txt.undraw()
            class2.undraw()
            txt1.undraw()
            class1.undraw()
            Mknight()
            break
        if 850<x<1103 and 265<y<525 :
            txt.undraw()
            class2.undraw()
            txt1.undraw()
            class1.undraw()
            Knight()
            break
Reply
#2
Assignment to "global" variables within functions does not change the variable in the outer scope - it creates new local variables with similar names.

The proper way is to pass the value to function as argument and to return value to the outer scope form your function.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(Jun-13-2018, 04:42 PM)volcano63 Wrote: Assignment to "global" variables within functions does not change the variable in the outer scope - it creates new local variables with similar names.

The proper way is to pass the value to function as argument and to return value to the outer scope form your function.
Can you give me an example please, im a noob at coding im doing this to learn python
Reply
#4
You've been bitten by the 'global feature' of Python. The following code will demonstrate one way to fix your problem.
def myFunction():
    p = True
    return

def myFunction2():
    global p
    p = True
    return

def myFunction3(p):
    p = True
    return p

p = False
myFunction()
print("p after myFunction = {}.".format(p))

p = False
myFunction2()
print("p after myFunction2 = {}.".format(p))

p = False
p = myFunction3(p)
print("p after myFunction3 = {}.".format(p))
Output:
p after myFunction = False. p after myFunction2 = True. p after myFunction3 = True.
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#5
Ok so I tried that but it wont go into landscape() now
from graphics import*
from pygame import*
import pygame
import graphics

##Variables##
x,y=0,0     #positions of mouse clicks
p1 = GraphWin("coord locator",1440,900)
Morange=False
Mpurple=False
if Mpurple==True:
    Landscape()
if Morange==True:
    Landscape()
##Main##
p1.setBackground('blue')
txt = Text(Point(725,125),'Select Your class')
txt.setTextColor('red')
txt.setSize(16)
txt.setFace('times roman')
txt.draw(p1)
txt1 = Text(Point(690,200),'Mounted Knight\t\t\t\t\t Knight')
txt1.setTextColor('red')
txt1.setSize(16)
txt1.setFace('times roman')
txt1.draw(p1)
class1=Image(Point(450,400),'Mknightlogo.gif')
class1.draw(p1)
class2=Image(Point(975,400),'Knightlogo.gif')
class2.draw(p1)
Mknighto=Image(Point(200,600),'Mknighto.gif')
Eknight=Image(Point(1200,700),'Eknight.gif')
Mknightp=Image(Point(200,600),'Mknightp.gif')

##Functions##

def Landscape():
    grass = Rectangle(Point(0,765),Point(1450,900))
    grass.setFill('green')
    grass.draw(p1)
    sky = Rectangle(Point(0,765),Point(1450,0))
    sky.setFill('sky blue')
    sky.draw(p1)
    sun=Image(Point(1200,200),'sun.gif')
    Eknight.draw(p1)
    while True:
        if Morange == True:
            Mknighto.draw(p1)
            print ('this is working sort of')
        if Mpurple == True:
            Mknightp.draw(p1)
            print ('this is working sort of')
def mknighto():
    global Morange
    Morange = True
    return
def mknightp():
    global Mpurple
    Mpurple = True
    return
def Mknight():
    p1.setBackground('green')
    txt.setText('You haven choosen your trusty study, choose your Colour')
    txt.draw(p1)
    txt1.setText('Orange\t\t\t\t\t Purple')
    txt1.draw(p1)
    txt1.move(40,0)
    clr=Image(Point(490,450),'orangecl.gif')
    clr.draw(p1)
    clr1=Image(Point(975,450),'purplecl.gif')
    clr1.draw(p1)
    while True:
        p=p1.checkMouse()  
        if p:
            x=p.getX()
            y=p.getY()
            print(x,y)
            if 290<x<690 and 250<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                mknighto()
                break
            if 775<x<1175 and 250<y<650 :
                txt.undraw()
                txt1.undraw()
                clr1.undraw()
                clr.undraw()
                mknighto()
                break
Reply
#6
Sorry nvm I just relieved my mistake what you told me helped appreciate +rep ljmetzger Pray
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this path work one way, but will the other way? cubangt 2 623 Sep-01-2023, 04:14 PM
Last Post: cubangt
  Why wont subprocess call work? steve_shambles 3 2,605 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  When I import a Module it wont run PyNovice 17 6,244 Oct-26-2019, 11:14 AM
Last Post: PyNovice
  Why wont this work? ejected 2 3,141 Mar-29-2019, 05:33 AM
Last Post: snippsat
  wont print last sentence.. mitmit293 2 2,324 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Why wont this work :( Nonagon 2 2,969 Nov-03-2018, 03:38 AM
Last Post: wavic
  Reddit Bot comes up with no errors but wont actually run Rubix3D 0 2,589 Jan-12-2018, 12:53 AM
Last Post: Rubix3D

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020