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.
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.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
make_character that:save_character that:make_character and save_character subprograms are called.Enter the name of the character: Elf
Enter health: 10
Enter attack: 13
Enter defend: 8
Character saved.
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()