Oct-03-2017, 06:20 PM
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
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