Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python + global variables = ;_;

Name: Anonymous 2007-09-02 6:17 ID:Fm5EpQWi

I'm new to python and 'real' programming. I fucked around with logo and qbasic before briefly, but never anything serious.

I want to change a variable from inside a function, but it only creates a local one with the same name. How the fuck do i change the global variable? (in this case "current_room").

please explain it to me or link to something useful.

;_;

in b4 people flip out and call me names + act like fags and bitch about python

Name: Anonymous 2007-09-02 7:47 ID:Fm5EpQWi

I figured it out. Allow me to reveal my terrible fucking code. Keep in mind this is my first program that does more than compare numbers or draw colored hexagons using turtles.

#------START MAGIC CODE
import math
import string
import sys
world = [["You're in a dark and spooky room! There's some stairs going up from here.", 0] , ["You are on the second floor of the exceptionally scary tower! There's a shower to [WASH] up in.", 3] , ["Now you are on the third floor. THE REALLY REALLY SCARY THIRD FLOOR, OH GOD!",0] , ["You're at the top of the tower. It's not that scary up here, but the ceiling is pretty low!\nSeems like you could probably [WIN] from here.",0]]
global starting_room
starting_room=0
current_room=0
pissed=0

#-------------------------------------------------------------------------------------------

def print_pre():
    print "\tWelcome to..."
    print
    print "\t\t\t*****************************************"
    print "\t\t\t*                                       *"
    print "\t\t\t*  D R A G O N ' S --- Q U E S T ! ! !  *"
    print "\t\t\t*                                       *"
    print "\t\t\t*****************************************"
    lines(1)
    print "\tAKA 'The wizards tower' (Gold Dragon expansion pack sold separately)"

#-------------------------------------------------------------------------------------------

def help():
    lines(1)
    print "[HELP]:\nTry going [U]p or [D]own! You can [Q]uit.\nYou can check your [STATUS] if you'd like to.\nThere is a way to [WIN], but only those who are pure may do so! \nAlso there's no dragon, but on the upside this wizard's tower is pretty cool."
    lines(1)

#-------------------------------------------------------------------------------------------

def stat():
    global pissed
    lines(1)
    print "Status:"
    if pissed != 1:
        print "You are feeling alright"
    if pissed == 1:
        print "Your pants are soaked in urine. Oops."
    lines(1)

#-------------------------------------------------------------------------------------------

def win():
    global pissed
    if current_room == 3 and pissed == 0:
        return 1
    elif current_room != 3:
        lines(1)
        print "You can't [WIN] from here. Try a more elevated location!"
        lines(1)
        return 0
    if current_room == 3 and pissed == 1:
        lines(1)
        print "You can't win with pissed pants!"
        lines(1)
        return 0

#-------------------------------------------------------------------------------------------

def wash():
    global pissed
    if current_room == 1 and pissed == 0:
        print "\nYou get all washed up and clean, but you can't help but feel weird getting nude in some strange wizard's shower.\n"
    if current_room == 1 and pissed == 1:
        print "\nYou get all washed up and clean, almost forgetting your earlier shame.\n"
        pissed = 2
    elif current_room != 1:
        print "\nWash up in what?\n"

#-------------------------------------------------------------------------------------------

def lines(x):
    while x>0:
        print
        x=x-1

#-------------------------------------------------------------------------------------------

def move(dir):
    global current_room
    if dir == "u" or dir=="up":
        if current_room + 2 > len(world):
            lines(1)
            print "You bumped your head! Ouch!"
            lines(1)
            return
        else:
            current_room = current_room + 1
    if dir == "d" or dir=="down":
        if current_room - 1 < 0:
            lines(1)
            print "There's no where to go down from here. You stop your feet angrily. Damn gravel!"
            lines(1)
            return
        else:
            current_room = current_room - 1       
    print_room()
    global pissed
    if current_room == 2 and pissed == 0:
        print "-You have pissed yourself-"
        pissed = 1
        lines(1)
    if current_room == 2 and pissed == 2:
        print "This room isn't so scary anymore.\nIt does smell pretty strongly of urine, however\n"

#-------------------------------------------------------------------------------------------

def prompt():
    print "health/mana >",
    x = raw_input()
    if x == "u" or x == "d" or x=="up" or x=="down":
        move(x)
        return 0
       
    elif x == "q" or x=="quit":
        lines(1)
        sys.exit("[QUIT]\n")
    elif x =="win":
        return win()       
    elif x =="help":
        help()
        return 0
    elif x =="status":
        stat()
        return 0
    elif x =="wash":
        wash()
        return 0
    else:
        lines(1)
        print "lol whut"
        print "Try [HELP]!"
        lines(1)
        return 0

#-------------------------------------------------------------------------------------------

def print_room():
    lines(1)
    print world[current_room][0]
    lines(1)

#-------------------------------------------------------------------------------------------

def print_mob():
    if world[current_room][1] < 1:
        return
    else:
        print "There are ", world[current_room][1], " monsters in this room!"


#-----------------START_GAME_HERE----------------------------------------

lines(25)
print_pre()
lines(2)
help()
print_room()

zoop = 0
while zoop == 0:
    zoop = prompt()

lines(1)
print "\t  ********************* YOU FUCKIN WIN!!!! *********************"
lines(5)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List