Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Load Game problem with my game

Sionus

New Coder
I'm attempting to make a text-based adventure game for fun and im having problems trying to load a specific file. Now before you cry im using repl.it, because i find it visually aesthetic for me. Anyways, what i have done is when you leave the game you can name a save file and one is created and saved. When you load in the game you are asked what save you want to load.
Python:
def LoadGame(Name, Health):
  global C
  global Level
  global Knowledge
  global CurrentArea
 
  print("Enter the name of your save")
  choice = input(">_ ")
  File_Test = choice + '.db'
  Does_Exist = exists(File_Test)
  test = os.path.isfile(File_Test)

  if Does_Exist == True:
    User = Player()
    Inventory = User.GetInventory()
    SaveFile = shelve.open(File_Test, writeback = True)
    Health = SaveFile["Health"]
    C = SaveFile["Credits"]
    Level = SaveFile["Level"]
    Inventory = SaveFile["Inventory"]
    Knowledge = SaveFile["Knowledge"]
    CurrentArea = SaveFile["CurrentArea"]
    Name = SaveFile["Name"]
    SaveFile.close()
    
    print("\n\x1B[3mGame loaded successfully\n\x1B[0m")
    print("Welcome back", Name)
    print("You are at:", CurrentArea)
    
    Game_Frame(Health, Name)
  else:
    print("Game file does not exist")

It presents me with this error:
Traceback (most recent call last):
File "main.py", line 1332, in <module>
DisplayMenu(100)
File "main.py", line 1320, in DisplayMenu
LoadGame(Name, Health)
File "main.py", line 74, in LoadGame
SaveFile = shelve.open(File_Test, writeback = True)
File "/usr/lib/python3.8/shelve.py", line 243, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/lib/python3.8/shelve.py", line 227, in __init__
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/usr/lib/python3.8/dbm/__init__.py", line 89, in open
raise error[0]("db type could not be determined")
dbm.error: db type could not be determined
 
What type of DB are you using? The error states that it can't determine the DB type. Something is amiss with how you're loading the DB. Best I can tell from available info anyways. Do you have the full code on github or anything?
 
Interesting that Python shows tracebacks with the most recent call last. That is unlike any other stack trace I've ever seen.
 
I can put the code on Github, the SaveFIle in particular just saves as [Name].db
I can't seem to replicate the error. I'm using python 3.8 It all seems to work pretty good, I've been able to save, it's auto saved and I've been able to load the savegame file. Also I see that in the code on Github has been updated since your post. When I load the game I just type in 'SaveFile' and it loads.

I've noticed that if you travel without entering a direction it gives you a list index out of range error. This can be solved by checking for a direction coming after your command of travel and if there's not a direction then you can warn that a direction of traveled is required.
 
Interesting that Python shows tracebacks with the most recent call last. That is unlike any other stack trace I've ever seen.
I think it's fairly intuitive that python does it that way. Most people will read from the top down instead of from the bottom up. So if the most recent call is last it logically follows the order that the code is running in. IMO anyways.
 
I think it's fairly intuitive that python does it that way. Most people will read from the top down instead of from the bottom up. So if the most recent call is last it logically follows the order that the code is running in. IMO anyways.
Yes, I'm not saying it's wrong. Just thought they'd stick to the accepted tradition of doing it bottom-up which is how a stack works, also intuitive, especially if you're used to it for so long. But it seems they call it a traceback rather than callstack or stacktrace so they could do it different 😉
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom