Python Forum
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data saving help
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.)
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
you may consider using json - easy to dump/load in a structured way
Reply
#6
In that case I may very well use classes in the main program but not in this little side program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help formatting dataframe data before saving to CSV cubangt 16 5,775 Jul-01-2022, 12:54 PM
Last Post: cubangt
  saving data from text file to CSV file in python having delimiter as space K11 1 2,391 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Reading serial data and saving to a file Mohan 1 7,523 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  Saving and accessing data from different clients SheeppOSU 1 1,971 Jul-28-2019, 02:15 AM
Last Post: metulburr
  Value Error and Saving Data to .csv caroline_d_124 1 2,276 Jan-06-2019, 10:53 PM
Last Post: stullis
  Need help saving data into MySQL database reezalaq 0 2,387 Jun-03-2018, 07:50 PM
Last Post: reezalaq
  Saving data in the Debugger macellan85 1 2,871 Apr-27-2017, 09:51 PM
Last Post: Larz60+
  Extracting data from tweets and saving it as CSV kiton 7 6,989 Feb-24-2017, 11:13 PM
Last Post: kiton

Forum Jump:

User Panel Messages

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