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