Hello, I'm back.
After lots of tinkering, and asking guys here at the forum, we made a program that could successfully create a dungeons and dragons role playing character. In the end, the output looks like this:
----------------------------------------------------------------
Your character's sex is: Male
Your character's name is: Charlie
Your character's race is: Gnome
Your character's class is: Warrior
----------------------------------------------------------------
.............Physical Traits............
strength = 14
decterity = 15
constitution = 10
.............Mental Traits...............
intelligence = 10
charisma = 9
wisdom = 8
----------------------------------------------------------------
Alignment: Chaotic Neutral
And that is in cmd of course.
The thing I was wondering was this, is it possible to write something into the python script to make it save this output as a .txt file?
Thanks, now I'm off to a birthday!
16 2281 bvdet 2,851
Expert Mod 2GB
Hello, I'm back.
After lots of tinkering, and asking guys here at the forum, we made a program that could successfully create a dungeons and dragons role playing character. In the end, the output looks like this:
----------------------------------------------------------------
Your character's sex is: Male
Your character's name is: Charlie
Your character's race is: Gnome
Your character's class is: Warrior
----------------------------------------------------------------
.............Physical Traits............
strength = 14
decterity = 15
constitution = 10
.............Mental Traits...............
intelligence = 10
charisma = 9
wisdom = 8
----------------------------------------------------------------
Alignment: Chaotic Neutral
And that is in cmd of course.
The thing I was wondering was this, is it possible to write something into the python script to make it save this output as a .txt file?
Thanks, now I'm off to a birthday!
Yes. This code - fn = r'X:\subdirectory\data.txt'
-
data = ['sex', 'name', 'race', 'class', 'strength', 'dexterity', 'constitution', 'intelligence', 'charisma', 'wisdom']
-
dd = dict.fromkeys(data, 'XX')
-
outList = ['%s = %s' % (item, dd[item]) for item in data]
-
f = open(fn, 'w')
-
f.write('\n'.join(outList))
-
f.close()
yields a file like this: sex = XX
name = XX
race = XX
class = XX
strength = XX
dexterity = XX
constitution = XX
intelligence = XX
charisma = XX
wisdom = XX
I'll leave it to you to pretty up the output.
or if its just one file, do it from the command line -
# python script.py > outfile.txt
-
Yes. This code - fn = r'X:\subdirectory\data.txt'
-
data = ['sex', 'name', 'race', 'class', 'strength', 'dexterity', 'constitution', 'intelligence', 'charisma', 'wisdom']
-
dd = dict.fromkeys(data, 'XX')
-
outList = ['%s = %s' % (item, dd[item]) for item in data]
-
f = open(fn, 'w')
-
f.write('\n'.join(outList))
-
f.close()
yields a file like this: sex = XX
name = XX
race = XX
class = XX
strength = XX
dexterity = XX
constitution = XX
intelligence = XX
charisma = XX
wisdom = XX
I'll leave it to you to pretty up the output.
That is probably good and all, but I have this insane theory that I will not get better at programming by just copying and pasting... Is that crazy? If you would please explain the different parts of that? Like the ones that are one step beyond basic? I'm a newbie, like it says under my name... If that saounded rude, it was unintentional.
Thanks
bvdet 2,851
Expert Mod 2GB
That is probably good and all, but I have this insane theory that I will not get better at programming by just copying and pasting... Is that crazy? If you would please explain the different parts of that? Like the ones that are one step beyond basic? I'm a newbie, like it says under my name... If that sounded rude, it was unintentional.
Thanks
You seem to have the right attitude, wanting to learn how it works. Writing information to a file is easy. Compiling the information to write to a file in a structured manner is not so easy (sometimes). Do you want to be able to read in the data at a later date? If so, it must be organized in a manner that will be easy to parse. I will explain my code, line for line.
This is the name of the file that we will write to: - fn = r'X:\subdirectory\data.txt'
This is a list of variable names: - data = ['sex', 'name', 'race', 'class', 'strength', 'dexterity', 'constitution', 'intelligence', 'charisma', 'wisdom']
A dictionary is probably the best way to organize data in many cases. The dictionary is Python's built-in mapping type object. The mapping type of object is also known as a hash table or associative array. We can initialize a dictionary to maintain your data in your program like this: - dd = dict.fromkeys(data, 'XX')
A player's attributes can be set like this: - >>> dd
-
{'dexterity': 'XX', 'strength': 'XX', 'name': 'XX', 'intelligence': 'XX', 'sex': 'XX', 'charisma': 'XX', 'race': 'XX', 'wisdom': 'XX', 'class': 'XX', 'constitution': 'XX'}
-
>>> dd['strength']=10
-
>>> dd
-
{'dexterity': 'XX', 'strength': 10, 'name': 'XX', 'intelligence': 'XX', 'sex': 'XX', 'charisma': 'XX', 'race': 'XX', 'wisdom': 'XX', 'class': 'XX', 'constitution': 'XX'}
-
>>> print dd['strength']
-
10
-
>>>
When you have gathered all of the player's data, create a list of formatted strings suitable for writing to disk: - >>> outList = ['%s = %s' % (item, dd[item]) for item in data]
-
>>> outList[4]
-
'strength = 10'
-
>>>
Create a file object for writing: join is a string method that is used to assemble a list of strings into a single string. I have chosen this method to further prepare the data for writing. Here's an example of the use of join: - >>> strList = ["This is one sentence.", "This is another sentence."]
-
>>>'\n'.join(strList)
-
>>> print '\n'.join(strList)
-
This is one sentence.
-
This is another sentence.
-
>>>
I am now writing the data to the file: - f.write('\n'.join(outList))
Close the file object:
This will read the data back in: - >>> dd1 = {}
-
>>> for item in open(fn).readlines():
-
... key, value = item.strip().split(' = ')
-
... dd1[key] = value
-
...
-
>>> for key in dd1:
-
... print '%s=%s' % (key, dd1[key])
-
...
-
dexterity=XX
-
strength=10
-
name=XX
-
intelligence=XX
-
sex=XX
-
charisma=XX
-
race=XX
-
wisdom=XX
-
class=XX
-
constitution=XX
-
>>>
... is the only thing that I still don't understand. I was without internet for some time, and bored out of my mind I tried to force pyton into telling me what those things did... it wasn't all easy... was rather hard to get the exact __doc__ for write, since it just wrote to the file, instead of giving info... This is what I made when I gave up understanding " '%s'='%s' ":
I'm posting the entire script, so it's rather a lot -
-
sex=raw_input("First, which sex do you want your character to be? ")
-
name=raw_input('What do you want to call your character? ')
-
-
import random
-
-
print ""
-
print ""
-
-
figures=[]
-
for n in range(6):
-
dice=[random.randint(1,6) for i in range(4)]
-
dice.sort()
-
d2=dice[1:]
-
figures.append(d2[0]+d2[1]+d2[2])
-
-
print figures
-
-
print ""
-
-
print """These are random numbers given by the dice,
-
you must now chose where to put them.
-
This will affect your game, and you should bear in mind
-
what kind of character you want to create."""
-
-
-
print ""
-
print ""
-
-
strength=int(raw_input("""Which one of them will you assign to strength?
-
Strength is a measure of muscle, endurance and stamina combined.
-
Strength affects the ability of characters to lift and carry weights and make effective melee attacks. """))
-
-
if int(strength) in figures:
-
figures.remove(strength)
-
else:
-
print "Don't try to cheat! Naughty! Everlasting curses upon thee!!"
-
while 1<2:
-
print "Cheater!"
-
print ""
-
-
print ""
-
print ""
-
print figures
-
print ""
-
print ""
-
-
-
dexterity=int(raw_input("""Which of them will you assign to Dexterity?
-
Dexterity encompasses a number of physical attributes including
-
hand-eye coordination, agility, reflexes, fine motor skills,
-
balance and speed of movement;
-
a high dexterity score indicates superiority in all these attributes.
-
Dexterity affects characters with regard to initiative in attack,
-
the projection of missiles from hand or other means,
-
and in defensive measures. Dexterity is the ability most influenced by
-
outside influences (such as armor) """))
-
-
if int(dexterity) in figures:
-
figures.remove(dexterity)
-
else:
-
print "Don't try to cheat! Naughty! Everlasting curses upon thee!!"
-
while 1<2:
-
print "Cheater!"
-
print ""
-
-
print ""
-
print ""
-
print figures
-
print ""
-
print ""
-
-
-
-
constitution=int(raw_input("""Which of them will you assign to Constitution?
-
Constitution is a term which encompasses the character's physique,
-
toughness, health and resistance to disease and poison.
-
The higher a character's Constitution, the more hit points
-
that character will have. Unlike the other ability scores,
-
which knock the character unconscious when they hit 0,
-
having 0 Constitution is fatal. """))
-
-
if int(constitution) in figures:
-
figures.remove(constitution)
-
else:
-
print "Don't try to cheat! Naughty! Everlasting curses upon thee!!"
-
while 1<2:
-
print "Cheater!"
-
print ""
-
-
-
print ""
-
print ""
-
print figures
-
print ""
-
print ""
-
-
-
-
intelligence=int(raw_input("""Which of them will to assign to Intelligence?
-
Intelligence is similar to IQ, but also includes mnemonic ability,
-
reasoning and learning ability outside those measured by the written word.
-
Intelligence dictates the number of languages a character
-
can learn and the number of spells a Wizard may know. """))
-
-
if int(intelligence) in figures:
-
figures.remove(intelligence)
-
else:
-
print "Don't try to cheat! Naughty! Everlasting curses upon thee!!"
-
while 1<2:
-
print "Cheater!"
-
print ""
-
-
-
print ""
-
print ""
-
print figures
-
print ""
-
print ""
-
-
-
-
wisdom=int(raw_input("""Which of them will you assign to Wisdom?
-
Wisdom is a composite term for the characters enlightenment,
-
judgement, wile, willpower and intuitiveness. """))
-
-
if int(wisdom) in figures:
-
figures.remove(wisdom)
-
else:
-
print "Don't try to cheat! Naughty! Everlasting curses upon thee!!"
-
while 1<2:
-
print "Cheater!"
-
print ""
-
-
-
print ""
-
print ""
-
print figures
-
print ""
-
print ""
-
-
-
-
print "Then the last one will be your character's Charisma."
-
print """Charisma is the measure of the character's combined physical attractiveness,
-
persuasiveness, and personal magnetism. A generally non-beautiful character
-
can have a very high charisma due to strong measures of
-
the other two aspects of charisma."""
-
charisma=figures[0]
-
del figures
-
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
-
-
print '....... Physical Traits .......'
-
print 'strength = %s ' % strength
-
print 'dexterity = %s ' % dexterity
-
print 'constitution = %s ' % constitution
-
print '....... Mental Traits .......'
-
print 'intelligence = %s ' % intelligence
-
print 'charisma = %s ' % charisma
-
print 'wisdom = %s ' % wisdom
-
print ''
-
-
if strength in (8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and dexterity in (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) and constitution in (11, 12, 13, 14, 15, 16, 17, 18) and charisma in (8, 9, 10, 11, 12, 13, 14, 15, 16, 17):
-
print 'You are eligible to be a dwarf.'
-
-
if dexterity in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and constitution in (7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and intelligence in (8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and charisma in (8, 9, 10, 11, 12, 13, 14, 15, 16, 17):
-
print 'You are eligible to be an elf.'
-
-
if strength in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and constitution in (8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and intelligence in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'You are eligilbe to be a gnome.'
-
-
if dexterity in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and constitution in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and intelligence in (4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'You are eligilbe to be a half-elf.'
-
-
if strength in (7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and dexterity in (7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and constitution in (10, 11, 12, 13, 14, 15, 16, 17, 18) and intelligence in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) and wisdom in (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17):
-
print 'You are eligilbe to be a halfling.'
-
-
print 'You are eligible to be a human.'
-
-
print ''
-
-
race=raw_input('Which race do you want your character to be? ')
-
-
if race is 'dwarf' or 'Dwarf':
-
constitution==constitution+1
-
charisma==charisma-1
-
-
if race is 'elf' or 'Elf':
-
dexterity=dexterity+1
-
constitution=constitution-1
-
-
if race is 'gnome' or 'Gnome':
-
intelligence=intelligence+1
-
wisdom=wisdom-1
-
-
if race is 'halfling' or 'Halfling':
-
dexterity=dexterity+1
-
strength=strength-1
-
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
-
print "your character's sex is %s" % sex
-
print "your character's race is %s" % race
-
print ''
-
print '....... Physical Traits .......'
-
print 'strength = %s ' % strength
-
print 'dexterity = %s ' % dexterity
-
print 'constitution = %s ' % constitution
-
print '....... Mental Traits .......'
-
print 'intelligence = %s ' % intelligence
-
print 'charisma = %s ' % charisma
-
print 'wisdom = %s ' % wisdom
-
print ''
-
-
-
if strength in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'you are eligible to be a warrior.'
-
-
if intelligence in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'you are eligilbe to be a mage.'
-
-
if wisdom in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'you are eligilbe to be a cleric.'
-
-
if dexterity in (9, 10, 11, 12, 13, 14, 15, 16, 17, 18):
-
print 'you are eligilbe to be a thief.'
-
-
print ''
-
-
class1=raw_input('Which class do you want to be? ')
-
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print '--------------------------------------'
-
print "Your character's sex is: %s" % sex
-
print "Your character's name is: %s" % name
-
print "Your character's race is: %s" % race
-
print "Your character's class is: %s" % class1
-
print '--------------------------------------'
-
print '....... Physical Traits .......'
-
print 'strength = %s ' % strength
-
print 'dexterity = %s ' % dexterity
-
print 'constitution = %s ' % constitution
-
print '....... Mental Traits .......'
-
print 'intelligence = %s ' % intelligence
-
print 'charisma = %s ' % charisma
-
print 'wisdom = %s ' % wisdom
-
print ''
-
yes="yes"
-
name2=raw_input("Now that you know this, do you wish to change your character's name? yes / no. ")
-
-
if name2==yes:
-
name=raw_input(" What do you wish to name your character? ")
-
else:
-
print ''
-
-
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
-
-
Lawful="Lawful"
-
Chaotic="Chaotic"
-
Neutral="Neutral"
-
Good="Good"
-
Evil="Evil"
-
lawful="lawful"
-
chaotic="chaotic"
-
neutral="neutral"
-
good="good"
-
evil="evil"
-
-
print '.......... Alignments 1: Law and Chaos .............'
-
print ''
-
-
-
-
for n in range(1,50):
-
aligninfo=raw_input("You may choose to be Lawful, Neutral or Chaotic. For more information, type in the alignment you want more information about. If you do not want any information and just want to get on with choosing, just press enter. ")
-
-
-
if aligninfo==Lawful:
-
print ""
-
print 'Lawful characters tell the truth, keep their words, respect authority, honor tradition and judge those who fall short of their duties.'
-
print ""
-
print ""
-
-
elif aligninfo==Chaotic:
-
print ""
-
print 'Chaotic characters follow their consciences, resent being told what to do, favor new ideas over tradition and do what they promise if they feel like it.'
-
print ""
-
print ""
-
-
elif aligninfo==Neutral:
-
print ""
-
print 'People who are neutral with respect to law and chaos have a normal respect for authority and feel neither a compulsion to obey nor to rebel, they are honest, but can be tempted into lying or decieving others.'
-
print ""
-
print ""
-
-
elif aligninfo==lawful:
-
print ""
-
print 'Lawful characters tell the truth, keep their words, respect authority, honor tradition and judge those who fall short of their duties.'
-
print ""
-
print ""
-
-
elif aligninfo==chaotic:
-
print ""
-
print 'Chaotic characters follow their consciences, resent being told what to do, favor new ideas over tradition and do what they promise if they feel like it.'
-
print ""
-
print ""
-
-
elif aligninfo==neutral:
-
print ""
-
print 'People who are neutral with respect to law and chaos have a normal respect for authority and feel neither a compulsion to obey nor to rebel, they are honest, but can be tempted into lying or decieving others.'
-
print ""
-
print ""
-
-
-
else:
-
print ""
-
print ""
-
break
-
-
al1=raw_input("Enter the alignment you have chosen. ")
-
print ""
-
print "Thank you! Moving on!"
-
-
-
-
-
print ""
-
print ""
-
print ".......... Alignments 2: Good versus Evil .........."
-
print ""
-
-
-
-
-
for n in range(1,50):
-
aligninfo=raw_input("You may choose to be either Good, Neutral or Evil. If you want some general info on the different alignments; type either Good, Neutral or Evil. If not, simply press enter. ")
-
-
-
-
if aligninfo==Good:
-
print ""
-
print '"Good" implies altruism, respect for life, and a concern for the dignity of sentinent beings. Good characters make personal sacrifises to help others.'
-
print ""
-
print ""
-
-
elif aligninfo==Neutral:
-
print ""
-
print "People who are neutral with respect to good and evil have compunctions against killing the innocent but lack the commitment to make sacrifices to protect or help others. Neutral people are committed to others through personal relationships. A neutral person may sacrifice himself to protect his family or even his homeland, but he would not do so for strangers who are not related to him."
-
print ""
-
print ""
-
-
elif aligninfo==Evil:
-
print ""
-
print '"Evil" implies hurting, oppressing and killing others. Some evil creatures simply have no compassion for others and would kill without qualms if doing so is convenient. Others actively pursue evil, killing for sport or out of duty to some evil deity or master'
-
print ""
-
print ""
-
-
elif aligninfo==good:
-
print ""
-
print '"Good" implies altruism, respect for life, and a concern for the dignity of sentinent beings. Good characters make personal sacrifises to help others.'
-
print ""
-
print ""
-
-
elif aligninfo==neutral:
-
print ""
-
print "People who are neutral with respect to good and evil have compunctions against killing the innocent but lack the commitment to make sacrifices to protect or help others. Neutral people are committed to others through personal relationships. A neutral person may sacrifice himself to protect his family or even his homeland, but he would not do so for strangers who are not related to him."
-
print ""
-
print ""
-
-
elif aligninfo==evil:
-
print ""
-
print '"Evil" implies hurting, oppressing and killing others. Some evil creatures simply have no compassion for others and would kill without qualms if doing so is convenient. Others actively pursue evil, killing for sport or out of duty to some evil deity or master'
-
print ""
-
print ""
-
-
else:
-
print ""
-
print ""
-
break
-
-
al2=raw_input("Type the alignment which you have chosen. ")
-
print ""
-
print ""
-
print ""
-
-
-
-
-
-
-
-
-
-
-
-
-
print ''
-
print ''
-
print ''
-
print ''
-
print 'Congratulations, you have successfully created a dungeons and dragons character.'
-
print '--------------------------------------'
-
print "Your character's sex is: %s" % sex
-
print "Your character's name is: %s" % name
-
print "Your character's race is: %s" % race
-
print "Your character's class is: %s" % class1
-
print '--------------------------------------'
-
print '....... Physical Traits .......'
-
print 'strength = %s ' % strength
-
print 'dexterity = %s ' % dexterity
-
print 'constitution = %s ' % constitution
-
print '....... Mental Traits .......'
-
print 'intelligence = %s ' % intelligence
-
print 'charisma = %s ' % charisma
-
print 'wisdom = %s ' % wisdom
-
print '--------------------------------------'
-
print 'Alignment:' , al1 , al2
-
print ''
-
print ''
-
print ''
-
-
-
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
outlist=[name,sex,race,class1,strength,dexterity,constitution,intelligence,charisma,wisdom,(al1+" "+al2)]
-
stuff=["Name","Sex","Race","Class","Strength","Dexterity","Constitution","Intelligence","Charisma","Wisdom","Alignment"]
-
finallist=['%s=%s' % (stuff[n],outlist[n]) for n in range(len(stuff))]
-
f=open(fn,'a')
-
f.write('\n'.join(finallist))
-
f.close()
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
f=open(fn,'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
f=open(fn,'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
f=open(fn,'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
-
-
-
-
-
for n in range(20):
-
if strength is n:
-
strength=str(n)
-
-
if dexterity is n:
-
dexterity=str(n)
-
-
if constitution is n:
-
constitution=str(n)
-
-
if intelligence is n:
-
intelligence=str(n)
-
-
if wisdom is n:
-
wisdom=str(n)
-
-
if charisma is n:
-
charisma=str(n)
-
-
-
-
-
-
-
-
-
fn=r'c:\programfiler\python script\character-pro.txt'
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
f=open(fn, 'a')
-
f.write("--------------------------------")
-
f.close()
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
f=open(fn, 'a')
-
f.write("This character's name is: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(name)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("It is a: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(race)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
f=open(fn, 'a')
-
f.write("It's sex is: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(sex)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
if sex=="female":
-
-
-
f=open(fn, 'a')
-
f.write("She is a: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(class1)
-
f.close()
-
-
-
elif sex=="Female":
-
-
-
f=open(fn, 'a')
-
f.write("She is a: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(class1)
-
f.close()
-
-
-
elif sex=="male":
-
-
-
f=open(fn, 'a')
-
f.write("He is a: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(class1)
-
f.close()
-
-
-
elif sex=="Male":
-
-
-
f=open(fn, 'a')
-
f.write("He is a: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(class1)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("--------------------------------")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("....... Physical Traits .......")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Strength: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(strength)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Dexterity: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(dexterity)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Constitution: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(constitution)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("........ Mental Traits ........")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Intelligence: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(intelligence)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Charisma: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(charisma)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Wisdom: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(wisdom)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("--------------------------------")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write("Alignment: ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(al1)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(" ")
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write(al2)
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
f=open(fn, 'a')
-
f.write('\n'.join('\n'))
-
f.close()
-
-
-
raw_input("You are finished! To quit this character-creator; press enter.")
-
-
I'm sorry for the lack of comments. It is a dice-based game, and this is a program for creating characters. I do not think the rules need be explained. The problem is the lower half, which I expect is vastly useless, and can be written in much shorter ways...
... is the only thing that I still don't understand. I was without internet for some time, and bored out of my mind I tried to force pyton into telling me what those things did... it wasn't all easy... was rather hard to get the exact __doc__ for write, since it just wrote to the file, instead of giving info... This is what I made when I gave up understanding " '%s'='%s' ":
I'm posting the entire script, so it's rather a lot
I'm sorry for the lack of comments. It is a dice-based game, and this is a program for creating characters. I do not think the rules need be explained. The problem is the lower half, which I expect is vastly useless, and can be written in much shorter ways...
And of course it doesn't allow me... I'll post it as a .txt file in case anyone of you are interested...
... is the only thing that I still don't understand. I was without internet for some time, and bored out of my mind I tried to force pyton into telling me what those things did... it wasn't all easy... was rather hard to get the exact __doc__ for write, since it just wrote to the file, instead of giving info... This is what I made when I gave up understanding " '%s'='%s' ":
I'm posting the entire script, so it's rather a lot
I'm sorry for the lack of comments. It is a dice-based game, and this is a program for creating characters. I do not think the rules need be explained. The problem is the lower half, which I expect is vastly useless, and can be written in much shorter ways...
And of course it doesn't allow me... I'll post it as a .txt file in case anyone of you are interested...
"%s = %s" just means string formatting. You need to specify some strings after the last quote using the percent:
print "'%s' = '%s'" % ("strenght", "4")
That would print:
"strength = 4"
Also, you have an open() and close() call for every call to write(). You only need to open and close the file once.
Also, you have an open() and close() call for every call to write(). You only need to open and close the file once.
Nope, I definitively do need to do it every bloody time, otherwise it claims that I haven't defined f... I know it should work without it, but it just doesn't...
*edit*= I found that by changing the f= open(fn,'a') into f= file(fn,'a'), I could write several times without having to close and redefine f...
Nope, I definitively do need to do it every bloody time, otherwise it claims that I haven't defined f... I know it should work without it, but it just doesn't...
*edit*= I found that by changing the f=open(fn,'a') into f=file(fn,'a'), I could write several times without having to close and redefine f...
A lot of things: - if race is 'dwarf' or 'Dwarf':
Is not what you want. - if race in ('dwarf', 'Dwarf'):
Is what you want.
and if your realy clever, You use: - if race.lower() == 'dwarf':
Then "dWaRF" would also be true.
And in the dwarf sction you got: - constitution==constitution+1
Thats bad. As you can se you are using the == operator.
Preferably you shuld use the += operator.
And your fine: - strength in (7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
Looks realy bad.
you can use the "in between" construction:
and as you use: -
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
A lot.
It be good if you did something like: -
newlines = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
-
# And then when you want a lot of newlines:
-
print newlines
-
And the print statements always (unless you use ",") end with a linebreak.
so:
do the same thing.
And if you want to use: -
while 1<2:
-
# a statement that is alwas true. Use:
-
while True:
-
And you dont need: - if int(wisdom) in figures:
-
#when its already an int. Do:
-
if wisdom in figures:
-
Keep it up ;)
you can only get better.
bvdet 2,851
Expert Mod 2GB
You can also print multiple new lines this way:
This works for any character or string.
bvdet 2,851
Expert Mod 2GB
Nope, I definitively do need to do it every bloody time, otherwise it claims that I haven't defined f... I know it should work without it, but it just doesn't...
*edit*= I found that by changing the f=open(fn,'a') into f=file(fn,'a'), I could write several times without having to close and redefine f...
Instead of doing this: - fn=r'c:\programfiler\python script\characters.txt'
-
outlist=[name,sex,race,class1,strength,dexterity,constitution,intelligence,charisma,wisdom,(al1+" "+al2)]
-
stuff=["Name","Sex","Race","Class","Strength","Dexterity","Constitution","Intelligence","Charisma","Wisdom","Alignment"]
-
finallist=['%s=%s' % (stuff[n],outlist[n]) for n in range(len(stuff))]
-
-
f=open(fn,'a')
-
f.write('\n'.join(finallist))
-
f.close()
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
f=open(fn,'a')
-
f.write('\n'.join('\n'))
-
f.close()
You can do this: - fn=r'c:\programfiler\python script\characters.txt'
-
outlist=[name,sex,race,class1,strength,dexterity,constitution,intelligence,charisma,wisdom,(al1+" "+al2)]
-
stuff=["Name","Sex","Race","Class","Strength","Dexterity","Constitution","Intelligence","Charisma","Wisdom","Alignment"]
-
finallist=['%s=%s' % (stuff[n],outlist[n]) for n in range(len(stuff))]
-
-
f = open(fn,'a') # open a file in append mode, create the file if it does not exist
-
f.write('\n'.join(finallist))
-
f.write('\n')
-
...do other stuff...
-
-
f.write('\nsome string\n')
-
f.writelines([s1, s1,s3]) # this writes a list of strings
-
f.close()
-
There is no difference in the use of open() and file(). From Python docs:
The file() constructor is new in Python 2.2. The previous spelling, open(), is retained for compatibility, and is an alias for file().
Instead of doing this: - fn=r'c:\programfiler\python script\characters.txt'
-
outlist=[name,sex,race,class1,strength,dexterity,constitution,intelligence,charisma,wisdom,(al1+" "+al2)]
-
stuff=["Name","Sex","Race","Class","Strength","Dexterity","Constitution","Intelligence","Charisma","Wisdom","Alignment"]
-
finallist=['%s=%s' % (stuff[n],outlist[n]) for n in range(len(stuff))]
-
-
f=open(fn,'a')
-
f.write('\n'.join(finallist))
-
f.close()
-
-
-
fn=r'c:\programfiler\python script\characters.txt'
-
f=open(fn,'a')
-
f.write('\n'.join('\n'))
-
f.close()
You can do this: - fn=r'c:\programfiler\python script\characters.txt'
-
outlist=[name,sex,race,class1,strength,dexterity,constitution,intelligence,charisma,wisdom,(al1+" "+al2)]
-
stuff=["Name","Sex","Race","Class","Strength","Dexterity","Constitution","Intelligence","Charisma","Wisdom","Alignment"]
-
finallist=['%s=%s' % (stuff[n],outlist[n]) for n in range(len(stuff))]
-
-
f = open(fn,'a') # open a file in append mode, create the file if it does not exist
-
f.write('\n'.join(finallist))
-
f.write('\n')
-
...do other stuff...
-
-
f.write('\nsome string\n')
-
f.writelines([s1, s1,s3]) # this writes a list of strings
-
f.close()
-
There is no difference in the use of open() and file(). From Python docs:
The file() constructor is new in Python 2.2. The previous spelling, open(), is retained for compatibility, and is an alias for file().
Well obviously there is some difference, for when I did the -
"f = open(fn,'a') # open a file in append mode, create the file if it does not exist
-
f.write('\n'.join(finallist))
-
f.write('\n')
-
...do other stuff...
-
-
f.write('\nsome string\n')"
-
-
thing, it gave me the error message "Line 'something', f.write(Some stuff)
f not specified
but when I specified f at every line, like this: -
-
f=open(fn, 'a')
-
f.write(some stuff)
-
f.close()
-
-
f=open(fn, 'a')
-
f.write(some other stuff)
-
f.close()
-
-
It worked perfectly, it was just annoying. When I spedified f as:
It didn't have to be specified and closed every time... Maybe my version is just buggy and glitchy, but whatever the reason, that is the way it is for me.
A lot of things: - if race is 'dwarf' or 'Dwarf':
Is not what you want. - if race in ('dwarf', 'Dwarf'):
Is what you want.
and if your realy clever, You use: - if race.lower() == 'dwarf':
Then "dWaRF" would also be true.
And in the dwarf sction you got: - constitution==constitution+1
Thats bad. As you can se you are using the == operator.
Preferably you shuld use the += operator.
And your fine: - strength in (7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
Looks realy bad.
you can use the "in between" construction:
and as you use: -
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
print ''
-
A lot.
It be good if you did something like: -
newlines = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
-
# And then when you want a lot of newlines:
-
print newlines
-
And the print statements always (unless you use ",") end with a linebreak.
so:
do the same thing.
And if you want to use: -
while 1<2:
-
# a statement that is alwas true. Use:
-
while True:
-
And you dont need: - if int(wisdom) in figures:
-
#when its already an int. Do:
-
if wisdom in figures:
-
Keep it up ;)
you can only get better.
Basically all of that was not made by me, and the int(wisdom) was done just because I knew it would work, and was tired of testing the entire script for every addition. The newlines="\n\n\n\n\n\n\n" was quite smart though, I shall have to remember that.
As for the operator +=... I've never seen it before, neither the race.lower()...
But thanks! You should talk to some admin or whatever and make them change the "newbie" there... It really doesn't suit you :P
You can also print multiple new lines this way:
This works for any character or string.
Thanks, I was kinda wondering about that... never "dared" try it tho :P
And instead of %s, couldn't I just have used str()?
That wouldn't confuse me as much :D
bvdet 2,851
Expert Mod 2GB
And instead of %s, couldn't I just have used str()?
That wouldn't confuse me as much :D
Sure, but if you want to learn Python, you should learn how to use the string format operator ' %'. Here's an example: - >>> print 'The first number is', 12, 'and the second number is', 44.5689
-
The first number is 12 and the second number is 44.5689
-
>>> s = 'The first number is ' + str(12) + ' and the second number is ' + str(44.5689)
-
>>> print s
-
The first number is 12 and the second number is 44.5689
-
>>> s = 'The first number is %s and the second number is %s' % (12,44.5689)
-
>>> s
-
'The first number is 12 and the second number is 44.5689'
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: simon.alexandre |
last post by:
Hi all,
I use csv module included in python 2.3. I use the writer and encouter the
following problem: in my output file (.csv) there is a duplication of the
end of line character, so when I open...
|
by: Samantha |
last post by:
Is there a limit on the size of the file Python will read then output. I am
reading a file of 433 lines and when I output the same file it only will
output 421 lines. The last line is cut off also....
|
by: Bob |
last post by:
Everybody,
I've been doing a lot of on-line research and cannot find
any reference to the exact problem I'm having.
Let me preface this question with the fact that I'm coming
from an Oracle...
|
by: tram |
last post by:
How do we pass on the step1 output file to step2 of the same job? I
would like to know the current job's output file programmetically,
instead of hard coding it. Any idaes would be appreciated.
...
|
by: yanakal |
last post by:
Hi,
I'm using isql to query data and output the same to a flat file.
The isql has the following command options ' -h-1 -w500 -n -b -s"" '.
In the SQL_CODE, the first two lines before the select...
|
by: Mountain Bikn' Guy |
last post by:
I am having serious problems with the following IDE bug:
Could not write to output file 'x.dll' -- 'The process cannot access the
file because it is being used by another process. '
and
BUG:...
|
by: Barry |
last post by:
Hi, guys
Basiclly, it is automated testing system. There is a main python script
that handles the testing campagin. This main script will call another
script that will in turn runs a few...
|
by: undshan |
last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:
#include...
|
by: jebbyleezer |
last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty.
Here is my source code:
import java.io.*;
import java.util.Scanner;...
|
by: dwaterpolo |
last post by:
Hi Everyone,
I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt
and compare them, the first text file has a bunch of values listed like:
y
y
y
y
y
y
y
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |