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


Messages In This Thread
Data saving help - by zedwardson - Oct-03-2017, 06:20 PM
RE: Data saving help - by ichabod801 - Oct-03-2017, 06:53 PM
RE: Data saving help - by zedwardson - Oct-03-2017, 06:59 PM
RE: Data saving help - by ichabod801 - Oct-03-2017, 07:06 PM
RE: Data saving help - by buran - Oct-03-2017, 07:09 PM
RE: Data saving help - by zedwardson - Oct-03-2017, 07:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help formatting dataframe data before saving to CSV cubangt 16 6,828 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,553 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Reading serial data and saving to a file Mohan 1 7,798 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  Saving and accessing data from different clients SheeppOSU 1 2,066 Jul-28-2019, 02:15 AM
Last Post: metulburr
  Value Error and Saving Data to .csv caroline_d_124 1 2,425 Jan-06-2019, 10:53 PM
Last Post: stullis
  Need help saving data into MySQL database reezalaq 0 2,476 Jun-03-2018, 07:50 PM
Last Post: reezalaq
  Saving data in the Debugger macellan85 1 2,981 Apr-27-2017, 09:51 PM
Last Post: Larz60+
  Extracting data from tweets and saving it as CSV kiton 7 7,207 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