Bottom Page

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()
Quote
#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.
Quote
#3
OP just PM he has found the problem and asked to delete the thread
Quote
#4
@OP
Then please enlighten us with your answer. We dont delete threads. All threads have merit for future googlers.
Quote
#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
Quote
#6
thats not wasting our time. It happens a lot. And it is worthwhile to have on the forum.
nilamo likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 244 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  Order of key and value in dictionary keeps changing palladium 2 158 Apr-02-2020, 07:59 AM
Last Post: perfringo
  Can't unpack values of dictionary with ** Snake 3 345 Mar-11-2020, 11:17 AM
Last Post: Snake
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 230 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  help with changing values in an excel sheet and saving to a new file Moneyman212 2 380 Nov-19-2019, 06:25 AM
Last Post: Moneyman212
  Python Script for parsing dictionary values from yaml file pawan6782 3 1,102 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 555 Jul-25-2019, 11:32 PM
Last Post: scidam
  update values in list based on dictionary bunti 3 1,026 Jun-10-2019, 07:26 AM
Last Post: perfringo
  randint stops changing values in a loop Naito 4 764 Jan-30-2019, 08:15 AM
Last Post: perfringo
  not not working looping through a dictionary rtbr17 2 568 Oct-04-2018, 01:37 PM
Last Post: rtbr17

Forum Jump:


Users browsing this thread: 1 Guest(s)