Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:22:50+00:00 2026-06-14T05:22:50+00:00

I’m making a combat helper for D&D. I plan to make it get the

  • 0

I’m making a combat helper for D&D. I plan to make it get the stats of each monster from a .txt file in this format:

_Name of monster_
HP = 45
AC = 19
Fort = -3

I’m using a class called Monster, and __init__ iterates through the .txt file. It iterates fine, my problem is that I can’t get the variables to have self. before it. Monsterfind() simply finds the path to the monster .txt file, and I know that is not the problem, as the variables are printing fine.

class Monster:
    def __init__(self, monster):
        """Checks if the monster is defined in the directory. 
        If it is, sets class attributes to be the monster's as decided in its .txt file"""
        self.name = monster.capitalize()
        monstercheck = self.monsterfind()
        if monstercheck != Fales:
            monsterchck = open(monstercheck, 'r')
            print monstercheck.next() # Print the _Name of Monsters, so it does not execute
            for stat in monstercheck:
                print 'self.{}'.format(stat) # This is to check it has the correct .txt file
                eval('self.{}'.format(stat))
            monstercheck.close()
            print 'Monster loaded'
        else: # if unfound
            print '{} not found, add it?'.format(self.name)
            if raw_input('Y/N\n').capitalize() == 'Y':
                self.addmonster() # Function that just makes a new file
            else:
                self.name = 'UNKNOWN'

It just says: self.AC = 5 SyntaxError: invalid syntax @ the equals sign

If there is any problem with my class or my __init__, even if it is unimportant, please tell me as this is the first time I’m using classes.

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:22:51+00:00Added an answer on June 14, 2026 at 5:22 am

    You don’t need eval() (or exec) here (they should pretty much never be used) – Python has setattr(), which does what you want.

    Note that it might be easier to use a data format that already exists, such as JSON, to avoid manually parsing it.

    As another note, when working with files, it’s best to use a context manager, as it reads nicely, and ensures a file is closed, even if there is an exception:

    with open(monstercheck, 'r') as monsterchck:
            print monstercheck.next()
            for stat, value in parse(monstercheck):
                setattr(self, stat, value)
    

    Obviously, you would need to do some real parsing here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.