![]() |
|
Beginners question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Beginners question (/thread-9106.html) |
Beginners question - Youmanity - Mar-21-2018 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---> error is where self.class_strenght() should be called This is my __main__ : import characterCreation
def main():
characterCreation.CharacterCreation().controller()
if __name__ == "__main__":
main()This is my characterCreation file:#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()
RE: Beginners question - Windspar - Mar-21-2018 To make a variable part of the object. You have to put self in front of it. self.prot = 'class'Otherwise it a local variable. You also misspelled strength. line 24 could also do. while(self.prot not in ['warrior', 'priest', 'thief', 'mage']): RE: Beginners question - Youmanity - Mar-21-2018 Ofc....didnt think of that. No scope no result:P And thanks for the while tip. Gonna test it out now:) RE: Beginners question - Windspar - Mar-21-2018 example . Could also do something like this. 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()
RE: Beginners question - Youmanity - Apr-08-2018 Sorry I missed your last post! But I got it fixed with your first:) Works like a charm. Thanks for your help:) |