Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginners question
#1
Hi. I am a guy with a Java background learning Python. I wrote a small text-based game. I created a __main__ class and a class called CharacterCreation. The second has two functions in it. Weird thing is, if I launch the program I am able to fire the first function, but an error occurs before the second is fired , self.class_strenght() . Could anyone please advice what Iam doing wrong?

This is my console output:
Quote:What is your name?d
My name is d
Nice to meet you d !
What class are you? You can choose warrior, priest, mage or thief!thief
thief
are you sure? Yes or No?yes
Error
---> error is where self.class_strenght() should be called

This is my __main__ :
1
2
3
4
5
6
7
import characterCreation
 
def main():
    characterCreation.CharacterCreation().controller()
   
if __name__ == "__main__":
    main()
This is my characterCreation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Character creation:
 
import random
import time
 
class CharacterCreation(object):
 
    def charCreate(self):
        protagonist_name = input('What is your name?')
        print('My name is', protagonist_name)
 
 
        time.sleep(1)
        print('Nice to meet you', protagonist_name,'!')
        time.sleep(1)
 
        flag = 'no';
         
        while (flag == 'no'):
            protagonist_class = input('What class are you? You can choose warrior, priest, mage or thief!')
            prot =  protagonist_class.lower()  
            print(prot)
                 
            while(prot !='warrior' and prot != 'priest' and prot != 'thief' and prot != 'mage'):
                print('You did not pick a class, please try again')
                protagonist_class = input('What class are you? You can choose warrior, priest, mage or thief!')
                prot =  protagonist_class.lower() 
          
            flag = input('are you sure? Yes or No?').lower()
             
           
         
    def class_strenght(self):
        prot = ''
        if prot == 'warrior':
            power = random.randint(10, 18)
            print('Your strenght is', power)
        elif prot == 'priest':
            power = random.randint(8, 16)
            print('Your strenght is', power)
        elif prot == 'thief':
            power = random.randint(6, 14)
            print('Your strenght is', power)
        elif prot == 'mage':
            power = random.randint(4, 12)
            print('Your strenght is', power)
        else:
            print('Error')
 
    def controller(self):
        self.charCreate()
        self.class_strenght()       
     
         
Reply
#2
To make a variable part of the object. You have to put self in front of it.
1
self.prot = 'class'
Otherwise it a local variable.

You also misspelled strength.

line 24 could also do.
1
while(self.prot not in ['warrior', 'priest', 'thief', 'mage']):
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Ofc....didnt think of that. No scope no result:P

And thanks for the while tip. Gonna test it out now:)
Reply
#4
example . Could also do something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import random
 
class Character:
    @classmethod
    def create(cls):
        character = cls()
 
        while len(character.name) < 3:
            character.name = input('What is your name? >> ')
 
        while character.klass not in ['warrior', 'priest', 'thief', 'mage']:
            character.klass = input('What class are you? You can choose warrior, priest, mage or thief! >> ')
            sure = input('Are you sure? Yes or No? >> ')
            if sure.lower() in ['no', 'n']:
                character.klass = ''
 
        if character.klass == 'warrior':
            character.power = random.randint(10, 18)
            character.health = random.randint(8, 12)
        elif character.klass == 'priest':
            character.power = random.randint(8, 16)
            character.health = random.randint(6, 10)
        elif character.klass == 'thief':
            character.power = random.randint(6, 14)
            character.health = random.randint(5, 8)
        elif character.klass == 'mage':
            character.power = random.randint(4, 12)
            character.health = random.randint(4, 6)
 
        return character
 
    def __init__(self):
        self.name = ''
        self.klass = ''
        self.power = 0
        self.health = 0
 
    def __repr__(self):
        return 'Character({name}, {klass}, {power}, {health})'.format(**vars(self))
 
def main():
    myCharacter = Character.create()
    print(myCharacter)
 
if __name__ == '__main__':
    main()
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Sorry I missed your last post! But I got it fixed with your first:) Works like a charm. Thanks for your help:)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020