battleman.py: refactored and added error checking to input code
This commit is contained in:
parent
5f5fe29032
commit
e40b485205
32
battleman.py
32
battleman.py
|
@ -23,7 +23,7 @@ class CombatGroup():
|
||||||
# the group
|
# the group
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_input(cls):
|
def from_input(cls):
|
||||||
name = raw_input("Name: ")
|
name = input_str("Name: ")
|
||||||
hp = input_int('hp')
|
hp = input_int('hp')
|
||||||
init_mod = input_int('init mod', 0)
|
init_mod = input_int('init mod', 0)
|
||||||
ap = input_int('action points', 0)
|
ap = input_int('action points', 0)
|
||||||
|
@ -32,7 +32,7 @@ class CombatGroup():
|
||||||
recharges = []
|
recharges = []
|
||||||
recharge = '-1'
|
recharge = '-1'
|
||||||
while True:
|
while True:
|
||||||
recharge = raw_input("recharge: ").split(',')
|
recharge = input_str("recharge: ").split(',')
|
||||||
if recharge == ['']:
|
if recharge == ['']:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
@ -137,18 +137,26 @@ class Combatant():
|
||||||
return "{} ({hp} hp)".format(self.name, hp=self.hp)
|
return "{} ({hp} hp)".format(self.name, hp=self.hp)
|
||||||
|
|
||||||
|
|
||||||
def input_int(prompt, default=-1):
|
def input_str(prompt, default=None, show_default=False, prompt_str=':'):
|
||||||
if default != -1:
|
full_prompt = prompt
|
||||||
prompt = prompt + ' [{}]'.format(default)
|
if default != None and show_default:
|
||||||
prompt = prompt + ': '
|
full_prompt = full_prompt + ' [{}]'.format(default)
|
||||||
|
full_prompt = full_prompt + '{} '.format(prompt_str)
|
||||||
data = raw_input(prompt)
|
|
||||||
if default != -1 and not data:
|
data = raw_input(full_prompt)
|
||||||
return default
|
if not data:
|
||||||
|
if default == None:
|
||||||
|
print 'Error: you must provide a value!'
|
||||||
|
return input_str(prompt, default, show_default, prompt_str)
|
||||||
|
else:
|
||||||
|
return default
|
||||||
else:
|
else:
|
||||||
return int(data)
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def input_int(prompt, default=None, show_default=True, prompt_str=':'):
|
||||||
|
return int(input_str(prompt, default, show_default))
|
||||||
|
|
||||||
|
|
||||||
# data about the battle - includes combatant list, etc
|
# data about the battle - includes combatant list, etc
|
||||||
class Battle():
|
class Battle():
|
||||||
|
@ -274,7 +282,7 @@ def main():
|
||||||
|
|
||||||
def do_prompt():
|
def do_prompt():
|
||||||
print('')
|
print('')
|
||||||
comm = raw_input('> ')
|
comm = input_str('', default='?', show_default=False, prompt_str='>')
|
||||||
|
|
||||||
if comm == '?':
|
if comm == '?':
|
||||||
do_help()
|
do_help()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user