Python Forum
Changing dictionary values not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Changing dictionary values not working (/thread-5999.html)



Changing dictionary values not working - SnekLover123 - Nov-01-2017

I'm making a text game, and for the crafting part, it seems to be working, but when I view the inventory, nothing changed. I can't see why it's ignoring the kenyan_sand_boa["blah blah blah"] = new value. I only posted the parts I thought could be causing the failure. Sorry there's so much code.

import os
import sys

#dictionary
kenyan_sand_boa = {
    "snek": "kenyan sand boa",
    "prey": "mice, " "birds, " "lizards, ",
    "biome": "desert",
    "predators": "desert monitor lizard",
    "6 months age": 182,
    "adult age": 365,
    "baby size": 10,
    "6 months size": 15,
    "adult size": 20,
    "current size": 10,
    "current days": 0,
    "current age": "baby",
    "name": "none",
    "current home": "none",
    "home size": 0,
    "health": 100,
    "max health": 100,
    "last ate": 0,
    "plants": 50,
    "wood": 50,
    "rocks": 50,
    "regeneration": 10,
}

def menu():
    os.system('cls')
    if kenyan_sand_boa["last ate"] >= 7:
        print "GAME OVER!!!! YOU STARVED TO DEATH!!!!! YOU SUCC!!!!!!!!!!!!!"
        os.system('pause')
        sys.exit()
    elif kenyan_sand_boa["current days"] == 182:
        print """Congradulations! You are 6 months old. Your size is now 15. If your home is
smaller than 15, then you will need to find a new home."""
        kenyan_sand_boa["current size"] = 15
        kenyan_sand_boa["current age"] = "6 months"
        os.system('pause')
        os.system('cls')
        print """--------------------------------------------------------------------------------
Health: %s  |  Day: %s  |  Last ate: %s days ago  |  Size: %s  |  Home size: %s

--------------------------------------------------------------------------------
What do you want to do?
 1. Read info about kenyan sand boas.
 2. Explore.
 3. Hunt.
 4. Crafting.
 5. View inventory.
 6. Save.
 7. Load save file.
 8. Exit.
 """ % (kenyan_sand_boa["health"], kenyan_sand_boa["current days"], kenyan_sand_boa["last ate"], kenyan_sand_boa["current size"], kenyan_sand_boa["home size"])
    elif kenyan_sand_boa["current days"] == 365:
        print """Congradulations! You are a fully grown adult. Your size is now 20. If your home
is smaller than 20, then you will need to find a new home."""
        kenyan_sand_boa["current size"] = 20
        kenyan_sand_boa["current age"] = "adult"
        os.system('pause')
        os.system('cls')
        print """--------------------------------------------------------------------------------
Health: %s  |  Day: %s  |  Last ate: %s days ago  |  Size: %s  |  Home size: %s

--------------------------------------------------------------------------------
What do you want to do?
 1. Read info about kenyan sand boas.
 2. Explore.
 3. Hunt.
 4. Crafting.
 5. View inventory.
 6. Save.
 7. Load save file.
 8. Exit.
 """ % (kenyan_sand_boa["health"], kenyan_sand_boa["current days"], kenyan_sand_boa["last ate"], kenyan_sand_boa["current size"], kenyan_sand_boa["home size"])
    else:
        print """--------------------------------------------------------------------------------
Health: %s  |  Day: %s  |  Last ate: %s days ago  |  Size: %s  |  Home size: %s

--------------------------------------------------------------------------------
What do you want to do?
 1. Read info about kenyan sand boas.
 2. Explore.
 3. Hunt.
 4. Crafting.
 5. View inventory.
 6. Save.
 7. Load save file.
 8. Exit.
 """ % (kenyan_sand_boa["health"], kenyan_sand_boa["current days"], kenyan_sand_boa["last ate"], kenyan_sand_boa["current size"], kenyan_sand_boa["home size"])


def explore():
    global monitor_health
    os.system('cls')
    print "You decide to slither around the desert"
    os.system('pause')
    os.system('cls')
    explore_int = random.randint(1,450)
    home_size = random.randint(7,14)
    monitor_health = random.randint(100,150)
    if 1 <= explore_int <= 15:
        print "A wild desert monitor lizard appeared!"
        os.system('pause')
        os.system('cls')
        snek_attack_monitor()
        menu()
    elif 16 <= explore_int <= 120:
        print "You found a burrow. Size: %s. Would you like to live in this burrow? y/n?" %home_size
        choose_home = raw_input()
        if choose_home == "y":
            if home_size < kenyan_sand_boa["current size"]:
                os.system('cls')
                print "This burrow is too small for you."
                os.system('pause')
                menu()
            else:
                os.system('cls')
                kenyan_sand_boa["home size"] = home_size
                kenyan_sand_boa["current home"] = "burrow"
                print "This is now your home."
                os.system('pause')
                menu()
        else:
            print "You decided not to live in the burrow."
            os.system('pause')
            menu()
    elif 21 <= explore_int <= 100:
        print "You found a log. Size: %s. Would you like to live in this log? y/n?" %home_size
        choose_home = raw_input()
        if choose_home == "y":
            if home_size < kenyan_sand_boa["current size"]:
                os.system('cls')
                print "This log is too small for you."
                os.system('pause')
                menu()
            else:
                os.system('cls')
                kenyan_sand_boa["home size"] = home_size
                kenyan_sand_boa["current home"] = "log"
                print "This is now your home."
                os.system('pause')
                menu()
        else:
            print "You decided not to live in the log."
            os.system('pause')
            menu()
    elif 101 <= explore_int <= 200:
        print "You found a cave. Size: %s. Would you like to live in this cave? y/n?" %home_size
        choose_home = raw_input()
        if choose_home == "y":
            if home_size < kenyan_sand_boa["current size"]:
                os.system('cls')
                print "This cave is too small for you."
                os.system('pause')
                menu()
            else:
                os.system('cls')
                kenyan_sand_boa["home size"] = home_size
                kenyan_sand_boa["current home"] = "cave"
                print "This is now your home."
                os.system('pause')
                menu()
        else:
            print "You decided not to live in the cave."
            os.system('pause')
            menu()
    elif 201 <= explore_int <= 250:
        print "You found a plant. Would you like to keep it? y/n?"
        plant = raw_input()
        if plant == "y":
            kenyan_sand_boa["plants"] += 1
            print "You decided to keep the plant."
            os.system('pause')
            menu()
        else:
            print "You decided not to keep the plant."
            os.system('pause')
            menu()
    elif 251 <= explore_int <= 350:
        print "You found a rock. Would you like to keep it? y/n?"
        rock = raw_input()
        if rock == "y":
            kenyan_sand_boa["rocks"] += 1
            print "You decided to keep the rock."
            os.system('pause')
            menu()
        else:
            print "You decided not to keep the rock"
            os.system('pause')
            menu()
    elif 351 <= explore_int <= 450:
        print "You found wood. Would you like to keep it? y/n?"
        wood = raw_input()
        if wood == "y":
            kenyan_sand_boa["wood"] += 1
            print "You decided to keep the wood."
            os.system('pause')
            menu()
        else:
            print "You decided not to keep the wood."
            os.system('pause')
            menu()

def crafting():
    if kenyan_sand_boa["rocks"] == 0 and kenyan_sand_boa["wood"]== 0 and kenyan_sand_boa["plants"] == 0:
        print "You don't have any items."
        os.system('pause')
        os.system('cls')
        menu()
    else:
        print """To craft, first pick how many rocks to use. Then pick how many wood to use. Then
pick how many plants to use.
"""
        os.system('pause')
        rocks = int(raw_input("Rocks: "))
        wood = int(raw_input("Wood: "))
        plants = int(raw_input("Plants: "))
        if rocks > 0 and wood == 0 and plants > 0:
            if rocks > kenyan_sand_boa["rocks"]:
                print "You don't have enough rocks."
                os.system('pause')
                menu()
            elif wood > kenyan_sand_boa["wood"]:
                print "You don't have enough wood."
                os.system('pause')
                menu()
            elif plants > kenyan_sand_boa["plants"]:
                print "You don't have enough plants."
                os.system('pause')
                menu()
            else:
                print "You created a size %s bio cave. It increases you health regeneration by 1 and your maximum health by %s." % (rocks, plants)
                choose_home = raw_input("Do you want to live in this bio cave? y/n?")
                if choose_home == "y":
                    if rocks < kenyan_sand_boa["current size"]:
                        print "The bio cave is too small."
                        os.system('pause')
                        menu()
                    elif rocks >= kenyan_sand_boa["current size"]:
                        kenyan_sand_boa["current home"] == "Bio cave"
                        kenyan_sand_boa["home size"] == rocks
                        kenyan_sand_boa["max health"] += plants
                        kenyan_sand_boa["regeneration"] += 1
                        kenyan_sand_boa["rocks"] -= rocks
                        kenyan_sand_boa["plants"] -= plants
                        print "This is now your home."
                        os.system('pause')
                        menu()
                else:
                    print "You decided not to live in the bio cave."
                    os.system('pause')
                    menu()
        if rocks == 0 and wood > 0 and plants > 0:
            if rocks > kenyan_sand_boa["rocks"]:
                print "You don't have enough rocks."
                os.system('pause')
                menu()
            elif wood > kenyan_sand_boa["wood"]:
                print "You don't have enough wood."
                os.system('pause')
                menu()
            elif plants > kenyan_sand_boa["plants"]:
                print "You don't have enough plants."
                os.system('pause')
                menu()
            else:
                print "You created a size %s bio log. It increases you health regeneration by 5 and your maximum health by %s." % (wood, plants)
                choose_home = raw_input("Do you want to live in this bio log? y/n?")
                if choose_home == "y":
                    if wood < kenyan_sand_boa["current size"]:
                        print "The bio log is too small."
                        os.system('pause')
                        menu()
                    elif wood >= kenyan_sand_boa["current size"]:
                        kenyan_sand_boa["current home"] == "Bio log"
                        kenyan_sand_boa["home size"] == wood
                        kenyan_sand_boa["max health"] += plants
                        kenyan_sand_boa["regeneration"] += 5
                        kenyan_sand_boa["wood"] -= wood
                        kenyan_sand_boa["plants"] -= plants
                        print "This is now your home."
                        os.system('pause')
                        menu()
                else:
                    print "You decided not to live in the bio log."
                    os.system('pause')
                    menu()



RE: Changing dictionary values not working - metulburr - Nov-01-2017

Quote:
        kenyan_sand_boa["current size"] = 15
        kenyan_sand_boa["current age"] = "6 months"

You need to pass the dictionary to and from every function that changes it. And to avoid passing i would use classes. Otherwise i cant tell without a working example.


RE: Changing dictionary values not working - buran - Nov-01-2017

OP just PM he has found the problem and asked to delete the thread


RE: Changing dictionary values not working - metulburr - Nov-01-2017

@OP
Then please enlighten us with your answer. We dont delete threads. All threads have merit for future googlers.


RE: Changing dictionary values not working - SnekLover123 - Nov-01-2017

Sorry for wasting your time. I figured out what was wrong. I put
kenyan_sand_boa["blah blah blah"] == new value
I was supposed to put
kenyan_sand_boa["blah blah blah"] = new value



RE: Changing dictionary values not working - metulburr - Nov-01-2017

thats not wasting our time. It happens a lot. And it is worthwhile to have on the forum.