import sys
from collections import namedtuple
Statistics = namedtuple('Statistics', 'strength, perception, endurance, charisma, intelligence, agility, luck')
def ask_for_value_in_list(prompt_message, *authorized_values):
while True:
value = input(prompt_message)
word = ''.join(filter(str.isalpha, value)).lower()
if word in authorized_values:
return word
if word == 'exit':
raise ValueError('asked to exit')
print('Only one of the following values is allowed:', authorized_values, file=sys.stderr)
def ask_for_integer(prompt_message):
while True:
value = input(prompt_message)
try:
return int(value)
except ValueError:
if value.lower() == 'stop':
raise
print('An integer is required!', file=sys.stderr)
def ask_for_stats(stat_name, max_points):
message = '[{} points left] {} >>> '.format(max_points, stat_name)
while True:
value = ask_for_integer(message)
if 1 <= value <= min(10, max_points):
return value
print('The provided value is out of bounds, try again.')
def build_statistics(max_points=26):
statistics = dict.fromkeys(Statistics._fields)
for statistic in statistics:
if not max_points:
raise ValueError('character exhausted statistics points')
value = ask_for_stats(statistic, max_points)
max_points -= value
statistics[statistic] = value
return Statistics(**statistics)
def main():
try:
stats = build_statistics()
except ValueError:
print('Could not build statistics, aborting.', file=sys.stderr)
else:
print('Statistics collected:', stats)
if __name__ == '__main__':
main()
I didn't put back the check for the strength being at least 10 points, but it should be easy enough to add it back if you really need it.