0

Well, I never thought of that, I think i did it like yours but inputed my own stuff, but I can not get it to break out of the loop when it is true..I just type in 1 so I set it to numbers. Here is the code, I know its simple fix but I cant figure it out.I did look this up, but it shows just about the same census as yours dose, except there 'while' is not indented at all.

def WhichCharacter(self):
        options = ['1']
        while True:
            choice = raw_input('Which Character?:')
            if choice in options == 1:
                return choice
            else:
                print 'Invalid choice'
0

if choice in options == 1 is redundant; choice in options already returns a boolean (True or False) if the value for choice is in the list options.

You need to change your options list to include the things that you want as valid input, like I did ('serf', 'baron', etc).

Also, when you check if choice is in options, convert choice to lower-case first so that if the user types "SERF" or "SeRf", etc, it'll be checked as "serf". (Just make sure that your options list has the valid inputs in lower-case too).

So if you make these changes (which makes your code like that code that I had posted), it should break out of the loop (and return the choice) once something valid is entered.

0

Should have seen that coming, somehow I knew that... Thanks again, for the 50th time =)

0

Argh! Another problem, I have no assembled another class for Character_2. And now in the options I put in the right info for the options to be true, but no matter if I put Serf, or Peasant, it prints both out.. I think it has something to do with the order of things, but Im not completely sure on this.. Here is the code, I basicly copy/pasted parts of Character_1 to make Character_2 and replaced info to reflect.

class MainMenu(object):
    def __init__(self):
        print 'Welcome to Dark Forest!'
        print 'Choose a Character to play'
        print '1)serf'
        print '2)peasant'
    def WhichCharacter(self):
        options = ['serf', 'peasant']

        while True:
            choice = raw_input('Which Character?(i.e serf):')
            if choice.lower() in options:
                return choice
            
            else:
                print 'Invalid choice'
            

MM = MainMenu()
choice = MM.WhichCharacter()



class Character_1:
    
    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 3
        self.defend = 5

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
        
            
self = Character_1('Serf')

self.level
print ' '
print 'You Picked Serf:\n'
print 'Here are your stats as of now.'
print 'Gold:',
print self.gold
print 'Level:',
print self.level
print 'Attack:',
print self.attack
print 'Defense:',
print self.defend

class Character_2:

    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 4
        self.defend = 3

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend

self = Character_2('Peasant')

self.level
print ' '
print 'You Picked Peasant:\n'
print 'Here are your stats as of now.'
print 'Gold:',
print self.gold
print 'Level:',
print self.level
print 'Attack:',
print self.attack
print 'Defense:',
print self.defend
0

Ah, a number of errors!

1. You need to understand that when you write a class, it won't get executed UNTIL you create an instance of it, i.e. var = myClass() . So you can define all your classes at the top of your script, and the order doesn't matter. What matters is when you create instances of them.

2. Please don't use self outside of the class - it makes your code really confusing. WITHIN the class, self refers to itself, but outside of the class, give it a different name that refers to the initialized instance you have created, i.e.:

class MyClass(object):
    def __init__(self, name):
        self.name = name
    def sayHi(self):
        # notice that when referring to anything in this class
        # from WITHIN itself, we use the 'self' name.
        print "Hello, my name is", self.name

# only when we call an instance of this class is its code executed
inst = MyClass("Frank")
# when referring to parts of the class from OUTSIDE
# of its declaration, we use the variable name we assigned it
# to. Preferrably not 'self' as that gets confusing.
print "The class' name is", inst.name
inst.sayHi()

3. Like I said, the script executes downwards. What you have in yours is:
- you make the main menu
- you get the choice
- you make a Character_1 class instance
- you make a Character_2 class instance
Declare all your classes at the top of your script (to keep things organized), then you can do something like I showed earlier:

mm = MainMenu()
choice = mm.getWhichClass()
if choice == 'serf':
    player = CharacterSerf()
elif choice == 'peasant':
    player = CharacterPeasant()
# etc...

You NEED these conditional (if) statements so that it will either create an instance of one class and assign it to player, or it will create the instance from the other class. Your code contains no if statements, so after getting the choice, it goes and creates a Character_1 instance, then after that does the same for Character_2.

Hope that cleared it up a little...

0

Thats all fine, I am still on the learning curve and pretty sure I am gettin on your nerves..But when I goto define my class Character_1 at the top of the script I get an error.
I put on the outside of class instead of self I put C1 & C2 for the classes. then at the top I try and put C1 = Character_1 to define the class and it comes back with error, Character_1 not defined.
I tried to figure this out yet again, before posting and asking yet for more help.. so here is some code.

class Character_1:
    
    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 3
        self.defend = 5

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
        
            
C1 = Character_1('Serf')

then at the top of the script, to define them. I tried to put () on them, and also the whole line Character_1('Serf')

C1 = Character_1 
C2 = Character_2
0

Sorry, I wasn't fully clear on what I meant by declaring them at the top. Think of the whole of each class block being a blueprint. So before you can make the thing, you need the blueprint first, i.e. you put the blocks of class declarations at the top, then below them (later in the script), you can make instances of them. Here's a template:

# put these CLASS blocks at the top
class Character_1(object):
    def __init__(self, char):
        self.char = char

class Character_2(object):
    def __init__(self, char):
        self.char = char

# now, we can make instances of them
inst1 = Character_1("serf")
inst2 = Character_2("peasant")

That's a rough structure. Below is code that WILL NOT work:

# no good!
inst = MyClass("Frank")

class MyClass(object):
    def __init__(self, name):
        self.name = name

The second code won't work because you told the interpreter (the thing 'reading' the code) to make an instance of MyClass BEFORE you gave it the blueprint for MyClass.

Your lines

C1 = Character_1
C2 = Character_2

are unnecessary because all that does is allow you to call the Character_1 class by using C1() now, as well as Character_1() .
Be careful! The parenthesis at the end make a call to that class, i.e. create an instance of it. If you don't have the parenthesis, you're just accessing the class object in memory, not calling it.

0

Alright, I am still not fully understanding how things are organized, or maybe its something else.
I fixed the 'self' problems. But I am tryin to remedy the calling class' before they are defined..
I seen that you split the MainMenu up by functions. and put the Class of characters in the middle. Is that what I need to do here? I know there is more issues most likely but I have fixed some.
Im learning tho...Sometimes I learn better by seeing than reading..

class MainMenu():
    def __init__(self):
        print 'Welcome to Dark Forest!'
        print 'Choose a Character to play'
        print '1)serf'
        print '2)peasant'
    def WhichCharacter(self):
        options = ['serf', 'peasant']

        while True:
            choice = raw_input('Which Character?(i.e serf):')
            if choice == 'peasant':
                player = Character_2
                break
                
            elif choice == 'serf':
                player = Character_1
                break
                
                
            
            else:
                print 'Invalid choice'
            

MM = MainMenu()
choice = MM.WhichCharacter()   

class Character_1:
    
    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 3
        self.defend = 5

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
        
            
C1 = Character_1('Serf')

C1.level
print ' '
print 'You Picked Serf:\n'
print 'Here are your stats as of now.'
print 'Gold:',
print C1.gold
print 'Level:',
print C1.level
print 'Attack:',
print C1.attack
print 'Defense:',
print C1.defend

class Character_2:

    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 4
        self.defend = 3

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
C2 = Character_2('Peasant')

C2.level
print ' '
print 'You Picked Peasant:\n'
print 'Here are your stats as of now.'
print 'Gold:',
print C2.gold
print 'Level:',
print C2.level
print 'Attack:',
print C2.attack
print 'Defense:',
print C2.defend
0

Your code will still be creating instances of both character classes. Like I said previously, you need conditional statements to handle whether something should be executed, or something else should in its place. What you're currently doing is creating an instance of Character_1, and then afterwards, creating an instance of Character_2.

Also, you were trying to initiate the player classes from within the main menu's WhichCharacter() function. This is wrong because the variable you store it in is local only to that scope and can't be accessed outside of that method. It's better to return the input to handle in the main scope of the program than try to mess with global variables.

I've fixed your code for you:

# We define all our class 'templates' up here.
# Remember, these aren't executed yet. They only
# are once we call on them during our program.
# These are just so the interpreter knows what a
# MainMenu class entails, etc.

class MainMenu():
    def __init__(self):
        print 'Welcome to Dark Forest!'
    def WhichCharacter(self):
        print 'Choose a Character to play'
        print '1)serf'
        print '2)peasant'
        options = ['serf', 'peasant']

        while True:
            # FIXED THIS:
            choice = raw_input('Which Character?(i.e serf):').lower()
            # If the input is in the list 'options':
            if choice in options:
                # return the input so that the global variable
                # 'choice' (from mm.WhichCharacter()) stores it.
                return choice
            else:
                print 'Invalid choice'
    
class Character_1:
    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 3
        self.defend = 5

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
    
class Character_2:
    def __init__(self,char):
        self.char = char
        self.gold = 150
        self.level = 1
        self.attack = 4
        self.defend = 3

    def Level_Up(self,x):
        self.level += 1
        self.gold += 125
        self.attack += 2
        self.defend += 1
        if self.level >= 1:
            print 'Level:',
            print self.level
            print 'Gold:',
            print self.gold
            print 'Attack',
            print self.attack
            print 'Defend:',
            print self.defend
    

# Now we do our program here.
# Make a MainMenu and get the user's choice.
MM = MainMenu()
choice = MM.WhichCharacter()

# We now have the user's input stored in
# the variable 'choice', so we can use
# if statements to make the 'player' variable
# an instance of the proper class.
if choice == "serf":
    player = Character_1('Serf')
elif choice == "peasant":
    player = Character_2('Peasant')
0

I see now, I was following the if/elif down below the character class' but I was not sure about keeping an' if' up in the MM class. But it makes since they way it is.. I think that it has to be this way because you have to call choice ==,after it has passed the Class' for Character_1 to be able to assign player to them.. I think thats right.

1

Dude, you're getting way ahead of yourself. This is not the right way to organize this.

Never copy and paste. Classes should not be thought of as unique characters. They are blueprints. A skyscraper is a skyscraper is a skyscraper. For example:

class Skyscraper:
      def __init__(self,height):
             self.height = height

SearsTower = Skyscraper(1000)
EmpireState = Skyscraper(2000)

The first game I wrote was contained completely in a while loop, with a main menu. Basic structure:

while 1:
     #options here
     user_option = raw_input("Choice: ")
     if user_option == "q":
          break
     #if statements for your options here

The classes your currently have aren't going to work. You need to understand code before you put in a script and expect it to work. If you don't understand our code, don't just copy it and go. Google, ask questions about it here, python documentation etc.

Votes + Comments
Good advice on understanding code before integrating
0

Well technically his classes DO work for now. Although the two Character classes share so much in common that it's better structured as a base Character class, and then more specialized Serf and Peasant classes which inherit from the Character class, and add their own unique properties.

But yes, the class declarations are the general terms of what you want (hence 'template' or 'blueprint'). They aren't the individual instances...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.