I'm learning Python by creating a text-based adventure.
I've put the stat creation and storage classes into separate files (modules?), and common functions (ValueError handling etc.) into another one as well.
Is this good practice?
import common_functions
# Maximum number of points. Adjust for balance later
statsMax = 26
stopMessage = "No. Stop. Try again."
# Class to store the stats of a character - both Players and NPCs
class statsStore():
def __init__(self):
self.S = 0 # Strength
self.P = 0 # Perception
self.E = 0 # Endurance
self.C = 0 # Charisma
self.It = 0 # Intelligence
self.A = 0 # Agility
self.L = 0 # Luck
# Class to ask players to customise their player stats, and display them.
class stats(statsStore):
def __init__(self):
super().__init__()
# Purely used for counting in the statsList function
self.x = 0
# Stats Functions
def statsFunc(self, stat, Text):
global stopMessage
global statsMax
if self.x == 0:
Limit = statsMax - 10
else:
Limit = statsMax - 20 - self.x
print("You have " + str(statsMax) + " points left.")
while True:
print(Text)
stat = common_functions.errorHandle_int(stat, ">>>")
if stat <= 10 and stat >= 1:
if (statsMax - stat) < Limit:
print(stopMessage)
continue
else:
statsMax = statsMax - stat
return stat
break
else:
print(stopMessage)
# What's Fallout?
def statsList(self):
global statsMax
self.statsDict = {"Strength": self.S,
"Perception": self.P,
"Endurance": self.E,
"Charisma": self.C,
"Intelligence": self.It,
"Agility": self. A,
"Luck": self.L}
self.attributes = [self.S, self.P, self.E,
self.C, self.It, self.A, self.L]
for statsTip, stat in self.statsDict.items():
self.statsDict[statsTip] = self.statsFunc(stat, statsTip)
self.attributes[self.x] = self.statsDict[statsTip]
# I need this block of code to make changes to the individual stats.
# Otherwise, printing self.S would output 0.
self.S = self.attributes[0]
self.P = self.attributes[1]
self.E = self.attributes[2]
self.C = self.attributes[3]
self.It = self.attributes[4]
self.A = self.attributes[5]
self.L = self.attributes[6]
self.x += 1
# print(self.statsDict)
# Printing a list looks neater
print(self.attributes)
# Stats Debug
Player1 = stats()
Player1.statsList()
print(Player1.S)
print(Player1.attributes)
The block of code where I set self.S = self.attribute[0]
(and for the other stats as well) seems inefficient, but it doesn't change those variables otherwise, even though it is changed in the dictionary/list.
Is there a cleaner way of implementing this?
Here is the common functions module as well:
import sys
stopMessage = "No. Stop. Try again."
def checkIn_list(List, Check):
global stopMessage
while True:
Check = "".join(
[letter for letter in input(">>>") if str.isalpha(letter)]).lower()
if Check == "exit":
sys.exit()
elif Check not in List:
print(List)
print(stopMessage)
continue
else:
return Check
break
def errorHandle_int(value, message):
global stopMessage
while True:
value = input(message)
if value.title() == "Stop":
sys.exit()
try:
value = int(value)
return value
break
except ValueError:
print(stopMessage)
# test_list = ("yes", "no", "stop")
# test_input = 0
# checkIn_list(test_list, test_input)
General code review is also appreciated!