0

I have made a Game which is like and RPG stat creator. However as part of the assignment, you have to print the stat onto a *.txt file.

I have got this far by using this website:

http://www.afterhoursprogramming.com/tutorial/Python/Writing-to-Files/

And the Code is:

   f = open("test.txt","a") #opens file with name of "test.txt"
   f.write("The Name of the Character is" ,name,)
   [Leave Line]
   [Leave Line]
   f.write("Class")
   [Leave Line]
   f.write("Strength /100:" ,strength,)
   [Leave Line]
   f.write("Magic /100:" ,magic,)
   [Leave Line]
   f.write("Dexterity /100: ,dexterity, ")
   [Leave Line]
   f.write("Extra Ability is...." ,extraability,)
   f.close()

But I don't know how to leave a line after each statistic. The end result should look like this:

enter image description here

The ,'something', are just variables I have made.

Help would be greatly appreciated!

Derek
  • 301
  • 2
  • 3
  • 7

3 Answers3

0

The same tutorial shows what the issue is -- you are missing a "\n" at the end of the string you write to the file. For blank lines, write just "\n".

To include variables as part of the output string, you have a number of options -- there's a good example here.

Community
  • 1
  • 1
jstevenco
  • 2,913
  • 2
  • 25
  • 36
0

As others already stated, your real problem here is the missing "\n". That said, I think a better solution than adding a "\n" for each line by yourself is writing what you want inside a list and then using a str.join:

content = [
    "The Name of the Character is" + name + "\n", # As first line has an extra blank line, this will be the only one with the explicit "\n"
    "Strength /100:" + strength,
    ...
]

base_name = "Frodo"
with open(base_name + ".txt","a") as f:
    f.write("\n".join(content)) # Maybe "\n\n" since you want a blank line between each attribute.

That should do the trick!

Fabio Menegazzo
  • 1,191
  • 8
  • 9
0

You could do this all in one file write as follows:

with open("test.txt", "a") as f:   #opens file with name of "test.txt"

    dPlayer = {
        "name" : "Bob",
        "class" : "Wizard",
        "strength" : 50,
        "magic" : 30,
        "dexterity" : 85, 
        "extraability" : "speed"}

    output = """The Name of the Character is {name}


Class: {class}

Strength: {strength}/100

Magic: {magic}/100

Dexterity: {dexterity}/100

Extra Ability is....{extraability}
"""

    f.write(output.format(**dPlayer))

This would give you an output file looking like:

The Name of the Character is Bob


Class: Wizard

Strength: 50/100

Magic: 30/100

Dexterity: 85/100

Extra Ability is....speed

I have stored the player's details into a single dictionary and the keys can be used as placeholders in the string. By using a multiline string with triple quotes, it makes it quite easy to format the output exactly as you need it.

Also by using a with statement, the file is automatically closed afterwards.

If you would rather not use a dictionary (or the newer formatting method), you could use the following, but extra care is then needed to make sure all of the arguments match the order of the string.

with open("test.txt", "a") as f:   #opens file with name of "test.txt"
    name = "Bob"
    player_class = "Wizard"
    strength =  50
    magic = 30
    dexterity = 85
    extraability = "speed"

    player = (name, player_class, strength, magic,  dexterity, extraability)
    output = """The Name of the Character is %s


Class: %s

Strength: %d/100

Magic: %d/100

Dexterity: %d/100

Extra Ability is....%s
""" % player

    f.write(output)

Tested using Python 2.7

Martin Evans
  • 45,791
  • 17
  • 81
  • 97