Hi! I am a newbie. Could someone tell me whats wrong with my code? Help!

I was making a text adventure game. I don’t know whats wrong with my code. Could someone help?

My code

player = input("Name: ")
Help = "NO CAPS    l=look    n=north   s=south     e=east    w=west    u=up    d=down   ew=eastwest    take (item)    l (item)    use (item)    r=restart"
inventory = ['lamp']

sect1 = ["l", "n", "s", "l door"]
sect2 = ["l", "e", "s", "l shelf"]
sect3 = ["l", "n", "s", "e", "w", "l shovel", "l flowers", "l gate", "take shovel"]
sect4 = ['l', 'w', 'n', 'e', 'sw', 'se']
sect5 = ['l', 's', 'nw', 'l shelf', 'l sofa', 'l TV', 'l TV stand', 'l table', 'l under table', 'l chairs', 'l cup', 'drink tea', 'take key', 'take remote']

sect7 = ['l', 'e']
sect8 = ['l', 'n']
sect9 = ['l', 'n']
sect10 = ['l', 'w', 'n']

dead = ['']


dis1 = "There is an door facing your north, behind you is an small garden"
dis2 = "Fortunately the door isn't locked, you opened it. You are in a doorway. Facing north is a shelf, go south if you want to exit this building, to your east is the hallway."
dis3 = "You are in a small garden, you can see some flowers and a sign beside saying NO PICKING FLOWERS. There is a shovel beside you. You can go north. Continue south out of the wooden gate to the sidewalk, east to proceed further into the garden. You can go west."
dis4 = "Your are in a hallway. There are two rooms in your se and sw, go east to the kitchen, west to the door way. Your north is the living room. "
dis5 = 'You are in the living room. there is a shelf and sofa behind you, on the opposite side of the sofa is the TV, there are some chair beside a table to your right. The bathroom is to your northwest.'

disdead = "Press enter to continue. "
Csect = sect1


def isect1():
    Csect = sect1
    print(dis1)

    while Csect == sect1:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        if Ans == "r":
            Csect = dead

        elif Ans == "inventory":
            print(inventory)


        elif Ans in sect1:

            if Ans in ("l", "look"):
                print(dis1)

            elif Ans == "n":
                Csect = sect2
                continue

            elif Ans == 'l door':
                print("it's just a ordinary wooden door. There is nothing special about it.")

            else:
                Csect = sect3
                continue

        else:
            print("Sorry, I don't recognize this command.")

def isect2():
    Csect = sect2
    print(dis2)

    while Csect == sect2:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        if Ans == "r":
            Csect = dead


        elif Ans in sect2:

            if Ans in ("l", "look"):
                print(dis2)

            elif Ans == "w":
                Csect = sect4
                continue

            elif Ans == "l shelf":
                print("You are facing the back of a wooden shelf. There is nothing special about it.")

            else:
                Csect = sect1
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass

def isect3():
    Csect = sect3
    print(dis3)

    while Csect == sect3:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        elif Ans == "r":
            Csect = dead

        elif Ans in sect3:

            if Ans in ("l", "look"):
                print(dis3)


            elif Ans == "n":
                Csect = sect1
                continue

            elif Ans == "s":
                Csect = "STREETS"
                continue

            elif Ans == "e":
                Csect = sect10
                continue

            elif Ans == "l shovel":
                print("Nothing special about it.")

            elif Ans == "l flowers":
                print("Nothing special about it.")

            elif Ans == "l gate":
                print("Nothing special about it.")

            elif Ans == "take shovel" and "shovel" not in inventory:
                inventory.append("shovel")
                print("taken")

            else:
                Csect = dead
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass

def isect4():
    Csect = sect4
    print(dis4)

    while Csect == sect4:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        if Ans == "r":
            Csect = dead

        elif Ans in sect4:

            if Ans in ("l", "look"):
                print(dis4)

            elif Ans == "e":
                Csect = sect2
                continue

            elif Ans == "n":
                Csect = sect5
                continue

            elif Ans == "se":
                Csect = sect9
                continue

            elif Ans == 'w':
                Csect = sect7
                continue

            else:
                Csect = sect8
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass



print("Hello "+player+"!")
print(" ")
print("Welcome to the land of Adventures!")
print(" ")
print("Type Help if you are stuck.")
print(" ")
print("-Front Porch-")
print('')

while Csect != dead:
    if Csect == sect1:
        isect1()
    elif Csect == sect2:
        isect2()
    elif Csect == sect3:
        isect3()
    elif Csect == sect4:
        isect4()


This is what happens when I run it:

Name: Python
Hello Python!
 
Welcome to the land of Adventures!
 
Type Help if you are stuck.
 
-Front Porch-

There is an door facing your north, behind you is an small garden
/n
There is an door facing your north, behind you is an small garden
/n
There is an door facing your north, behind you is an small garden
/s
There is an door facing your north, behind you is an small garden
/s
There is an door facing your north, behind you is an small garden
/

Thanks!

You have a local variable Csect and a global variable Csect, which are independent from each other. Changing the value of the local variable Csect has no effect on the global variable Csect.

Global and Local Variables in Python - GeeksforGeeks has a good explanation that may help you.

1 Like

You can add a code block into the functions where you use Csect(this code will work properly if you place it before the function code.)

global Csect

By this way, your changes on Csect will be saved. So, problem is the scope.

1 Like

In general, you should avoid using global whenever possible (which is almost always), as it makes your code much more difficult to read, understand, modify and debug, as it means anything anywhere can modify that variable rather than it being a set input to your function. Instead, you can pass variables you need in and out of functions, or encapsulate your data in a class instance.

1 Like

Hey! May I ask if I am supposed to do it like this?

player = input("Name: ")
Help = "NO CAPS    l=look    n=north   s=south     e=east    w=west    u=up    d=down   ew=eastwest    take (item)    l (item)    use (item)    r=restart"
inventory = ['lamp']


sect1 = ["l", "n", "s", "l door"]
sect2 = ["l", "e", "s", "l shelf"]
sect3 = ["l", "n", "s", "e", "w", "l shovel", "l flowers", "l gate", "take shovel"]
sect4 = ['l', 'w', 'n', 'e', 'sw', 'se']
sect5 = ['l', 's', 'nw', 'l shelf', 'l sofa', 'l TV', 'l TV stand', 'l table', 'l under table', 'l chairs', 'l cup', 'drink tea', 'take key', 'take remote']

sect7 = ['l', 'e']
sect8 = ['l', 'n']
sect9 = ['l', 'n']
sect10 = ['l', 'w', 'n']

dead = ['']


dis1 = "There is an door facing your north, behind you is an small garden"
dis2 = "Fortunately the door isn't locked, you opened it. You are in a doorway. Facing north is a shelf, go south if you want to exit this building, to your east is the hallway."
dis3 = "You are in a small garden, you can see some flowers and a sign beside saying NO PICKING FLOWERS. There is a shovel beside you. You can go north. Continue south out of the wooden gate to the sidewalk, east to proceed further into the garden. You can go west."
dis4 = "Your are in a hallway. There are two rooms in your se and sw, go east to the kitchen, west to the door way. Your north is the living room. "
dis5 = 'You are in the living room. there is a shelf and sofa behind you, on the opposite side of the sofa is the TV, there are some chair beside a table to your right. The bathroom is to your northwest.'

disdead = "Press enter to continue. "

Csect = sect1


def isect1():
    Csect = sect1
    print(dis1)

    while Csect == sect1:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        if Ans == "r":
            global Csect
            Csect = dead

        elif Ans == "inventory":
            print(inventory)


        elif Ans in sect1:

            if Ans in ("l", "look"):
                print(dis1)

            elif Ans == "n":
                global Csect
                Csect = sect2
                continue

            elif Ans == 'l door':
                print("it's just a ordinary wooden door. There is nothing special about it.")

            else:
                global Csect
                Csect = sect3
                continue

        else:
            print("Sorry, I don't recognize this command.")

def isect2():
    Csect = sect2
    print(dis2)

    while Csect == sect2:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        if Ans == "r":
            global Csect
            Csect = dead


        elif Ans in sect2:

            if Ans in ("l", "look"):
                print(dis2)

            elif Ans == "w":
                global Csect
                Csect = sect4
                continue

            elif Ans == "l shelf":
                print("You are facing the back of a wooden shelf. There is nothing special about it.")

            else:
                global Csect
                Csect = sect1
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass

def isect3():
    Csect = sect3
    print(dis3)

    while Csect == sect3:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        elif Ans == "r":
            global Csect
            Csect = dead

        elif Ans in sect3:

            if Ans in ("l", "look"):
                print(dis3)


            elif Ans == "n":
                global Csect
                Csect = sect1
                continue

            elif Ans == "s":
                global Csect
                Csect = "STREETS"
                continue

            elif Ans == "e":
                global Csect
                Csect = sect10
                continue

            elif Ans == "l shovel":
                print("Nothing special about it.")

            elif Ans == "l flowers":
                print("Nothing special about it.")

            elif Ans == "l gate":
                print("Nothing special about it.")

            elif Ans == "take shovel" and "shovel" not in inventory:
                inventory.append("shovel")
                print("taken")

            else:
                global Csect
                Csect = dead
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass

def isect4():
    Csect = sect4
    print(dis4)

    while Csect == sect4:
        Ans = input("/")

        if Ans == "help":
            print(Help)

        elif Ans == "inventory":
            print(inventory)

        if Ans == "r":
            global Csect
            Csect = dead

        elif Ans in sect4:

            if Ans in ("l", "look"):
                print(dis4)

            elif Ans == "e":
                global Csect
                Csect = sect2
                continue

            elif Ans == "n":
                global Csect
                Csect = sect5
                continue

            elif Ans == "se":
                global Csect
                Csect = sect9
                continue

            elif Ans == 'w':
                global Csect
                Csect = sect7
                continue

            else:
                global Csect
                Csect = sect8
                continue

        else:
            print("Sorry, I don't recognize this command.")
            pass



print("Hello "+player+"!")
print(" ")
print("Welcome to the land of Adventures!")
print(" ")
print("Type Help if you are stuck.")
print(" ")
print("-Front Porch-")
print('')

while Csect != dead:
    if Csect == sect1:
        isect1()
    elif Csect == sect2:
        isect2()
    elif Csect == sect3:
        isect3()
    elif Csect == sect4:
        isect4()


It has this error when I run it.

  File "/Users/apple/PycharmProjects/Adventure/Home.py", line 42
    global Csect
    ^
SyntaxError: name 'Csect' is used prior to global declaration

Process finished with exit code 1

If you read the error message,

it tells you exactly what is going on. You’ve used Csect on the first line of the isectl function, but then only declare it a global later on. As @soil correctly stated when mentioning that you could use global,

I,e, as the error message states, you must define Csect to be global inside your function body first before doing anything else with it.

1 Like

Thanks a lot everyone! It runs fine now! :smiley: