Python Forum
Data saving help - 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: Data saving help (/thread-5435.html)



Data saving help - zedwardson - Oct-03-2017

okay all, first post!

I started learning python (2.7) as a hobby. Was a CS minor 10 years ago but not touched coding since, and now I have a general question.

I have written code (copied below) that generates some random stats and takes in input from the user to create a "character" for a game. I want to be able to save generated data as a file somehow. It can be a individual file (then preferably using the charictername string as a filename) or be appended into a single file, but then I need to be able pull this data easily.

(The goal is that this code generates characters for a game, and writes them to a file. Then I have a 2nd program that loads these characters and actually uses them in play.)

Thanks for all the help!
Code below

import random

def rollstat():  # this rolls stats per T&T 
    roll_one = random.randint(1,6)
    roll_two = random.randint(1,6)
    roll_three = random.randint(1,6)
    if roll_one == roll_two and roll_one == roll_three: #checks to see if all rolls are equal
        taro = rollstat() #recurse!
    else:
        taro = 0
    stat = roll_one + roll_two + roll_three + taro
    return stat #returns a value 

def getadds(st, dex, lk, spd):  #figures out adds 
  addstr = 0
  adddex = 0
  addlk = 0
  addspd = 0

  if st > 12:
    addstr = st-12 
  if dex > 12:
    adddex = dex-12
  if lk > 12:
    addlk = lk-12 
  if spd > 12: 
    addspd = spd-12

  charadds = addspd+addlk+adddex+addstr
  return charadds

print "Generating Charicter" #rolls the 9 stats
st = rollstat()
dex = rollstat()
con = rollstat()
wiz = rollstat()
lk = rollstat()
iq = rollstat()
spd = rollstat()
cha = rollstat()
san = rollstat()

print "STR " + str(st)
print "DEX " + str(dex)
print "CON " + str(con)
print "WIZ " + str(wiz)
print "LK " + str(lk)
print "IQ " + str(iq)
print "SPD " + str(spd)
print "CHA " + str(cha)
print "SAN " + str(san)

print "What you you want to name this Charicter?"  #races will be easy to add
charictername = raw_input()
print "What race is this Charicter, 1 - Human, 2 - Dwarf, 3 - Elf, 4 - Hobb"
charicterrace = input()
racename = "Human"
if charicterrace == 2:
  st = st+4
  dex = dex+4
  lk = lk-2
  wiz = wiz-2
  racename = "Dwarf"
if charicterrace == 3:
  con = con-1
  dex = dex+1 
  iq = iq+2
  wiz = wiz+2
  cha = cha+2 
  racename = "Elf"
if charicterrace == 4:
  st = st-2
  con = con+2
  dex = dex+2
  lk = lk-2
  racename = "Hobb"
if charicterrace == 1:
  racename = "Human"
  lk = lk+5

print "What class? 1 - Fighter, 2 - Cleric, 3 - Thief, 4 - Wizard" #classes less so 
charicterlass = input()
archtype = "Fighter"

if charicterlass == 2:
  archtype = "Cleric"
if charicterlass == 3:
  archtype = "Theif"
if charicterlass == 4:
  archtype = "Wizard"

adds = getadds(st, dex, lk, spd)
charictergold = 0
xpoints = 0

print "What god does he/she worship? 1 - Adpeh, 2 - Miphic, 3 - Kralic, 4 - Vassa 5 - Harkas 6 - Xi 7 - Yulia 8 - Yerik"
godname = input()

worshipedgod = "Adpeh"

if godname == 2:
  worshipedgod = "Miphic"
if godname == 3:
  worshipedgod = "Kralic"
if godname == 4:
  worshipedgod = "Vassa"
if godname == 5:
  worshipedgod = "Harkas"
if godname == 6:
  worshipedgod = "Xi"
if godname == 7:
  worshipedgod = "Yulia"
if godname == 8:
  worshipedgod = "Yerik"


print charictername  #printing a charicter sheet
print racename +" " + archtype
print "Combat Adds " + str(adds)
print "Worships " + str(worshipedgod)
print "STR: " + str(st)
print "DEX: " + str(dex)
print "CON: " + str(con)
print "WIZ: " + str(wiz)
print "LK:  " + str(lk)
print "IQ:  " + str(iq)
print "SPD: " + str(spd)
print "CHA: " + str(cha)
print "SAN: " + str(san)
print "XP:  " + str(xpoints)
print "Gold: " + str(charictergold)
print ""
print ""
foofoger = raw_input("press enter to continue")

curst = st   #moving stats to the current stats, which may change over time.  
curdex = dex
curcon = con 
curwiz = wiz
curlk = lk
curiq = iq
curspd = spd 
curcha = cha
cursan = san 

armorname = "rags" #name of current armor 
armorvalue = 0  #DR of current armor 
weild = "fist" #current wepon 
weilddie = 0 #number of d6 it rolls 
weildadd = 1  #number of adds 
ringslots = 2 #rings 
amuletslot = 1 #amulet 
daystoadv = 0  #number of days before they can adventure (wounds, healing)
Prayer = 10  #prayer cutoff 
hunger = 20 #hunger counter
lightwound = 0  #light wounds (on return to town can add to daystoadv or add a scar )
seriouswound = 0 #more serious then light 
criticalwound = 0  #very serious 
scars_wounds = ""  #text of scars and wounds from previous wounds



RE: Data saving help - ichabod801 - Oct-03-2017

A simple way to do it is just write it tab separated:

with open('character.txt', 'w') as character_file:
    character_file.write('str\t{}\n'.format(str))
    character_file.write('dex\t{}\n'.format(dex))
    ...
Then you can load them:

with open('character.txt') as character_file:
    for line in character_file:
        attribute, value = line.split()
        if attribute == 'str':
            str = int(value)
        ...
Two pieces of advice: learn classes (there's a link to a tutorial in my signature, below) and upgrade to Python 3.6.


RE: Data saving help - zedwardson - Oct-03-2017

(Oct-03-2017, 06:53 PM)ichabod801 Wrote: A simple way to do it is just write it tab separated:

with open('character.txt', 'w') as character_file:
    character_file.write('str\t{}\n'.format(str))
    character_file.write('dex\t{}\n'.format(dex))
    ...
Then you can load them:

with open('character.txt') as character_file:
    for line in character_file:
        attribute, value = line.split()
        if attribute == 'str':
            str = int(value)
        ...
Two pieces of advice: learn classes (there's a link to a tutorial in my signature, below) and upgrade to Python 3.6.

Main reason using 2.7 is that I have access to a book on 2.7. If I was to put this data into a class can I save it just as a class and then load it as a class? (I did OOP in Java years ago but on my first day in Python.)


RE: Data saving help - ichabod801 - Oct-03-2017

You could use pickle to save the class, but I find it better to write specific save and load methods. With classes you can use getattr and setattr to put the saving and loading into loops. If you don't want go full on OOP yet, dictionaries are a good stepping stone. They can also be saved/loaded with a loop pretty easily.


RE: Data saving help - buran - Oct-03-2017

you may consider using json - easy to dump/load in a structured way


RE: Data saving help - zedwardson - Oct-03-2017

In that case I may very well use classes in the main program but not in this little side program.