Hi! I am a newbie. Why doesn't list.replace() work? Help!

Hey! I was working on a text adventure game and was trying to remove a part of dis3 after the player takes the item in isect3().

Here is my code

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   drop (item)    take all"
inventory = ['$10 bill']
appearance = "dirty"

#1

sect1 = ["l", "n", "s", "l door"]
sect2 = ["l", "w", "s", "l shelf"]
sect3 = ["l", "n", "s", "e", "w", "l shovel", "l flowers", "l gate", "take shovel", "take flowers"]
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 silver key', 'take mask', 'l book','take book', 'push book', "take brass key"]
sect6 = ['l', 'e', 'take all', 'take shampoo', 'take toothpaste', 'take toothbrush', 'l mirror', 'take shower', 'shower', 'l comb', 'take comb']
sect7 = ['l', 'se', 'l cutlery', 'l knife', 'take knife']
sect8 = ['l', 'ne', 'take orange key']
sect9 = ['l', 'n']
sect10 = ['l', 'w', 'n']

dead = ['']

#2

dis1 = "There is an door facing your north, behind you is an small garden"
dis2 = "Facing north is a shelf, go south if you want to exit this building, to your west is the hallway."
dis3 = "You are in a small garden, you can see some flowers in it. 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.'
dis6 = "You are in the bathroom. There is a bottle of shampoo, a comb, a toothbrush and a toothpaste on the shelf. There is a mirror and shower."
dis7 = "You are in the kitchen. There are some cutlery around and a nice looking knife on the counter. Go southeast to exit the kitchen."

disdead = "Press enter to continue. "

Csect = sect1

#3

def isect1():
    global Csect
    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():
    global Csect
    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.")

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

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

def isect3():
    global sect3
    global dis3
    global Csect
    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("They are genuinely beautiful!")

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

            elif Ans == "take flowers" and "flowers" not in inventory:
                dis3.replace(", you can see some flowers in it", '')
                sect3.remove("take flowers")
                inventory.append("flowers")
                print("taken")

            elif Ans == "take shovel" and "shovel" not in inventory:
                dis3.replace("There is a shovel beside you. ", '')
                sect3.remove("take shovel")
                inventory.append("shovel")
                print("taken")

            else:
                Csect = dead
                print("GAME OVER")
                print('')
                print("You have stepped on wet cement!")
                continue

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

def isect4():
    global Csect
    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

def isect5():
    global sect5
    global dis5
    global Csect
    Csect = sect5
    print(dis5)

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

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

        if Ans == "r":
            Csect = dead

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


        elif Ans in sect5:

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

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

            elif Ans == 'l shelf':
                print("There are some books on it. There is a specific book which has no title. It seems suspicious.")

            elif Ans == 'l book':
                print("The book has no title. It seems weird.")

            elif Ans == 'take book':
                Csect = dead
                print('The lights goes out. You could hear a thump of the door closing. You feel a gush of wind. A voice bellowed "HOW DARE YOU TOUCH THE FORBIDDEN BOOK OF ADVENTURES!"')
                print('')
                print('')
                print('')
                print('')
                print('GAME OVER')
            elif Ans == 'push book':
                print("You pushed the book. You hear a crank then a rumble. The shelf slides aside, revealing a lit stairway going down.")
                sect5.append("d")
                dis5 = 'You are in the living room. There is a secret entrance to a stairway leading downward.  There is a 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.'
                sect5.remove("l shelf")
                sect5.remove("l book")
                sect5.remove("take book")
                sect5.remove("push book")

            elif Ans == "l under table":
                print("You look under the table. You see a brass key below it.")

            elif Ans == "take brass key" and "brass key" not in inventory:
                print('taken')
                sect5.remove("take brass key")
                inventory.append("brass key")

            elif Ans == 'l key':
                print("It's a shiny brass key.")

            elif Ans == 'l sofa':
                print("It seems very comfy, you really want to enjoy it, but you remind yourself that you are not here to watch TV.")

            elif Ans == 'l TV':
                print("There is nothing special about it.")

            elif Ans == "l chairs":
                print("You see nothing special about them")

            elif Ans == 'l TV stand':
                print("You see an a silver key and mask inside it.")

            elif Ans == 'l table':
                print("You see a cup of tea on it.")

            elif Ans == 'take silver key' and "silver key" not in inventory:
                if "orange key" in inventory:
                    print("taken")
                    inventory.append("silver key")
                else:
                    print("The cabinet is locked. Find the key first")

            elif Ans == 'take mask' and "mask" not in inventory:
                if "orange key" in inventory:
                    print("taken")
                    inventory.append("N95 mask")
                else:
                    print("The cabinet is locked. Find the key first")

            elif Ans == 'l cup':
                print("It's a nice teacup.")

            elif Ans == "drink tea":
                print("You drank it. Thankfully there wasn't poison in it.")

            elif Ans == "nw":
                Csect = sect6
                continue

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

def isect6():
    global Csect
    global appearance
    Csect = sect6
    print(dis6)

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

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

        if Ans == "r":
            Csect = dead

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

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

        elif Ans in sect6:

            if Ans == "e":
                Csect = sect5
                continue

            elif Ans == 'l mirror':
                if appearance == "dirty":
                    print("You saw a reflection of yourself, you look pretty dirty. You think that you need a shower.")
                else:
                    print("You look at the mirror. You look tidy and clean.")

            elif Ans in ('take shower', 'shower'):
                if appearance == 'dirty':
                    appearance = "clean"
                    print('You took a shower and groomed yourself.')
                else:
                    print("You are good, you do not need a shower.")

            elif Ans == "take all" and ("shampoo", 'toothbrush', 'toothpaste') not in inventory:
                inventory.append("shampoo")
                inventory.append('toothpaste')
                inventory.append('toothbrush')
                print('taken')

            elif Ans == "take shampoo":
                inventory.append("shampoo")
                print("taken")

            elif Ans == "take toothbrush":
                inventory.append("toothbrush")
                print("taken")

            elif Ans == "take toothpaste":
                inventory.append("toothpaste")
                print("taken")

            elif Ans == "l toothpaste":
                print("I see nothing special about that.")

            elif Ans == "l toothbrush":
                print("You hope it wasn't used by someone else.")

            elif Ans == "l shampoo":
                print("I see nothing special about that.")

            elif Ans == "l comb":
                print("It seems clean and new.")

            elif Ans == "take comb":
                print("taken")
                inventory.append("comb")


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



def game():
    global Csect
    while Csect != dead:
        if Csect == sect1:
            isect1()
        elif Csect == sect2:
            isect2()
        elif Csect == sect3:
            isect3()
        elif Csect == sect4:
            isect4()
        elif Csect == sect5:
            isect5()
        elif Csect == sect6:
            isect6()

    print('')
    print(disdead)
    while Csect == dead:
        Ans = input('')
        if Ans == "":
            Csect = sect1
            game()
        else:
            pass


#5

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

game()

This is what happens when I run it.

Hello!
 
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
/s
You are in a small garden, you can see some flowers in it. 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.
/take flowers
taken
/l
You are in a small garden, you can see some flowers in it. 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.
/take shovel
taken
/l
You are in a small garden, you can see some flowers in it. 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.
/

Thanks!

I think it would be a lot more maintainable if you just looped over the items when you need to “look” and print a description of the ones that are in the room. Then you don’t have to try to manipulate a string when you take and drop items.

But your specific problem is that dis3 is not a list, it’s a string. And str.replace() doesn’t modify the string, it returns a new string with the modification. You need to assign it to something to use it in the future.

>>> s = "ABC"
>>> s.replace("B", "") # returns a modified string, but does not change s
'AC'
>>> s # unchanged
'ABC'
>>> s = s.replace("B", "") # reassigning to capture the change
>>> s # now the original is modified.
'AC'
1 Like

Thanks a lot! It works now! :smiley: :smiley:

Hi “Python and ladders”,

list.replace doesn’t work because it doesn’t exist:

mylist = [1, 2, 3]
mylist.replace(2, 999)

# Fails with AttributeError: 'list' object has no attribute 'replace'

In your code, you are calling the replace method of a string, not a
list. You have code like this:

dis3.replace(something, newthing)

where dis3 is a string. Unfortunately, all that does is make a copy of the original string with the replacement, then throws it away immediately because you don’t save it anywhere.

You need to do this:

dis3 = dis3.replace(something, newthing)

All the string methods like replace(), upper(), strip() etc return a new string, they don’t change the existing one.

By the way, is “dis” supposed to be short for description? “desc” might be a better abbreviation.

In the future, please don’t dump hundreds and hundreds of lines of code in our lap and expect use to work out what part of it is not doing what you expect. We’re volunteers, working for free.

Please read this page for help in asking good questions that are more respectful of the time and effort of the volunteers answering:

Thank you, and happy coding!

1 Like

Thanks! I will try to improve on that!