简体   繁体   中英

How to export Python 3.4 input information to JSON?

I am trying to build an artificial intelligence program that will keep track of information and other stuff, but am having issues with trying to get it to export the information to aa file. I am new to Python and would like some help identifying what could be the issue.

Here is the source code.

#Importations
import jsonpickle
import math
import os
import sys
import time
from random import randrange, uniform
#Setups
SAVEGAME_FILENAME = 'aisetup.json'
game_state = dict()
#Program AI
class Computer(object):
    def __init__(self, name, creator):
        self.name = name
        self.creator = creator
#User Information
class Human(object):
    def __init__(self, name, birth):
        self.name = name
        self.birth = birth       
#Load Program Save
def load_game():
    """Load game state from a predefined savegame location and return the
    game state contained in that savegame.
    """
    with open(SAVEGAME_FILENAME, 'r') as savegame:
        state = jsonpickle.decode(savegame.read())
    return state
#Save Program to JSON
def save_game():
    """Save the current game state to a savegame in a predefined location.
    """
    global game_state
    with open(SAVEGAME_FILENAME, 'w') as savegame:
        savegame.write(jsonpickle.encode(game_state))
#Initialize Program
def initialize_game():
    """Runs if no AISave is found"""
    UserProg = Human('User', '0/0/0')
    AISystem = Computer('TempAI', 'Austin Hargis')

    state = dict()
    state['UserProg'] = [UserProg]
    state['AISystem'] = [AISystem]
    return state
#TextPad
#Main Code - Runs Multiple Times per Being Openned
def prog_loop():
    global game_state
    name = input (str("What is your name?: "))
    save_game()

#Main Program
def main():
    """Main function. Check if a savegame exists, and if so, load it. Otherwise
    initialize the game state with defaults. Finally, start the game.
    """
    global game_state

    if not os.path.isfile(SAVEGAME_FILENAME):
        game_state = initialize_game()
    else:
        game_state = load_game()
    prog_loop()
#Launch Code
if __name__ == '__main__':
    main()

Every time I run this, it exports the information to a file like this:

{"UserProg": [{"birth": "0/0/0", "py/object": "__main__.Human", "name": "User"}], "AISystem": [{"py/object": "__main__.Computer", "name": "TempAI", "creator": "Austin Hargis"}]}

I want it to export your name to the folder but it does not work right.

You never do anything with the name the user enters:

def prog_loop():
    global game_state
    name = input (str("What is your name?: "))
    save_game()

name is just a local variable there. If you wanted to save that as the name for the human, then you need to set that on the UserProg entry in your game state:

def prog_loop():
    name = input("What is your name?: ")
    # game_state['UserProg'] is a list with one Human instance in it
    game_state['UserProg'][0].name = name
    save_game()

Because you are altering the mutable object contained in game_state rather than assign to game_state itself, you don't need the global statement there. The str() call was also redundant, the "..." syntax already produced a string, there is little point in converting that to a string again .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM