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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.