Creating a player character.

★☆☆

Role playing games require players to adopt a virtual character in the game. Characters often have traits recorded as numerical values for strength, dexterity, constitution, intelligence, wisdom and charisma. This program creates a simple character and saves it to a file.

Attributes

Make

Write a program that asks the user to enter the name of a character, together with their health, attack and defend as numbers. The program should write the data to a file.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called make_character that:

  1. Asks the user to input the name of the character.
  2. Asks the user to input stats for: health, attack and defend values.
  3. Stats must be validated as numbers to be accepted.
  4. Returns a list with the name and stats.

Complete the subprogram called save_character that:

  1. Takes a list of attributes as a parameter.
  2. Opens a file for writing data. The name of the file is the name of the character + “.txt”
  3. Writes “[Name]” to the file followed by the character name on a new line, followed by a blank line.
  4. Writes “[Attributes]” to the file followed by each attribute on a new line.
  5. Closes the file.

Complete the main program so that:

  1. The make_character and save_character subprograms are called.
  2. Outputs “Character saved.”

Typical inputs and outputs from the program would be:

Enter the name of the character: Elf
Enter health: 10
Enter attack: 13
Enter defend: 8
Character saved.

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.

🆘 If you're really stuck, use this Parsons code sorting exercise
make_character
# Input the attributes for the character
def make_character():
---
    character_name = input("Enter the name of the character: ")
    health = ""
    attack = ""
    defend = ""
---
    # Ensure stats are numbers
    while not health.isdigit():
---
        health = input("Enter health: ")
---
    while not attack.isdigit():
---
        attack = input("Enter attack: ")
---
    while not defend.isdigit():
---
        defend = input("Enter defend: ")
---
    
    # Build the return list
    attributes = [character_name, health, attack, defend]
---
    return attributes
make_character
# Save the character to a file
def save_character(attributes):
---
    file_name = attributes[0] + ".txt"
---
    file = open(file_name, "w")
---
    
    # Name section
    file.write("[Name]\n")
---
    character_name = attributes[0] + "\n"
---
    file.write(character_name)
---
    file.write("\n")
---
    
    # Stats section
    file.write("[Attributes]\n")
---
    health = "health=" + attributes[1] + "\n"
---
    file.write(health)
---
    attack = "attack=" + attributes[2] + "\n"
---
    file.write(attack)
---
    defend = "defend=" + attributes[3] + "\n"
---
    file.write(defend)
---
    file.close()