Python Forum
Changing dictionary values not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing dictionary values not working
#1
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()
Reply
#2
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.
Recommended Tutorials:
Reply
#3
OP just PM he has found the problem and asked to delete the thread
Reply
#4
@OP
Then please enlighten us with your answer. We dont delete threads. All threads have merit for future googlers.
Recommended Tutorials:
Reply
#5
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
Reply
#6
thats not wasting our time. It happens a lot. And it is worthwhile to have on the forum.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  ttp template not working when values are absent in some scenarios jss 0 342 Oct-31-2023, 09:01 PM
Last Post: jss
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,695 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,517 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 2,956 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,904 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,428 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 1,896 Oct-29-2020, 12:32 AM
Last Post: Pedroski55
  Counting the values ​​in the dictionary Inkanus 7 3,516 Oct-26-2020, 01:28 PM
Last Post: Inkanus

Forum Jump:

User Panel Messages

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