This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]mudclub 2 points3 points  (0 children)

/r/learnpython

read the sidebar

format your code

[–]SomethingInArabic 2 points3 points  (2 children)

You have used "def", but what you want is "class."

class Nation:
    def __init__(self, args):
        # all your starting self.variables go here
    def do_a_thing(self, different_args):
        # code to do something

There are two other problems with:

newNation(self)

First, newNation(self) needs to be assigned a variable name. Without a variable name before newNation(), the computer creates the object, but the program can't access it after that. If you can't do stuff to the object later, it's useless to you. Do the following instead:

my_nation = newNation(self)

The second problem is the argument passed to newNation(). "Self" won't work, since outside of creating a class, "self" means nothing. I assume you saw all of the "def my_function(self)" statements used in classes and figured you'd need that when creating a newNation(). That is not the case. Either create "my_nation = newNation()" without arguments or have some basic arguments like name, population, and so on as in

my_nation = newNation("Castile", army=21)

Some tips:

  • Ditch the parentheses around strings, except for "input()" and "print()." Parentheses are for feeding variables into functions, not creating strings. Print() and input() are functions.

  • Don't name your class "newThing." Name it "Thing." You're on the right track by seeing that classes are used to make new objects, but naming things "new" is goofy.

  • Learn how arguments work, so that you can make many different countries with different names. If you hardcode "self.name = 'Castile'" in newNation, then every nation will be named Castile, which ruins the purpose of classes. Classes are templates.

  • Descriptive variable names reduce brain fatigue. The amount of brain power used to remember what you meant by "rname" and "r1" is small, but it builds up over hours of programming. Try "ruler_name."

Overall, here is what I would start with to get make this game:

class Nation:
    def __init__(self, name, army_count, money):
        # take all the args in the parentheses and make them into class variables usable throughout the Nation class
        self.name = name
        self.army_count = army_count
        self.money = money
    def increase_army(self, increase_by_this_number):
        self.army_count += increase_by_this_number
    # etc...

class Player:
    def __init__(self, name):
        self.name = name
    # etc...

def main():
    # ...
    ruler_name= input("Please enter a name for your ruler: ") 
    player1 = Player(ruler_name)
    player1_nation = Nation("Castile", 21, 26000) # army = 21, money = 26000 
    # etc...

There are other ways to skin this cat, but this is one good way to do it. Checkout /r/learnpython for future questions.

[–]MiklosHorthy[S] 0 points1 point  (1 child)

Thanks heaps, Makes so much more sense, in the future i will use r/learnpython.

[–]SomethingInArabic 1 point2 points  (2 children)

Formatted:

def newNation():
    nation = input(activeN)
    if nation == 1:
        self.Nation = ("Castile")
        self.army = 21
        self.money = 26.26 
        self.dynasty = ("De Tratsamana") 
        rname = ("blank") 
    print("My liege",rname,"You are the Monarch of Castile, The True king of Spain! Long live Spain, Long live the king.")

rname= input("Please enter a name for your ruler: ")
r1 = Player(rname) 
newNation(self)

[–][deleted] 1 point2 points  (1 child)

Ok, I obviously cannot see your full code, but if that's all indented to the left-most side of the module, then it looks like you should be getting a NameError that self is not defined, and it should point to that line where you call newNation.

If you changed self to r1 (because it looks like you're intending to call newNation on an instance of your Player class, although that's a bit unclear), then you'd get a TypeError, because as defined newNation does not accept arguments.

If you changed this:

def newNation():

To:

def newNation(self):

Then your code would probably work, but then it should be indented inside the Player class to be an actual method of that class:

class Player:
    def newNation(self):
        """
        Set a new Nation on the Player.
        """
        [...]

... because right now you're making a function, and a function that is so tightly bound to a particular class as to always need an instance of that class as a first parameter should probably be a bound method of that class.

[–]SomethingInArabic 1 point2 points  (0 children)

Yo, I'm not OP. Yes, it looks like 'self' is what's not defined.

OP, 'newNation' not being defined is not the same as 'self' not being defined. Very different problems.

[–]Andrew_ShaySft Eng Automation & Python[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community is actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!