Skip to content

Commit

Permalink
Added input validation to admin.py menus, and a "Quit without Save" o…
Browse files Browse the repository at this point in the history
…ption.
  • Loading branch information
IanWitham committed Apr 27, 2012
1 parent 55dd8f4 commit 6555a2a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
50 changes: 46 additions & 4 deletions admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def save_and_quit(self):
f.close()
self.running = False

def quit_without_save(self):
is_sure_about_quitting = self.yes_no_prompt(
"Are you super sure you want to quit without saving?"
)
if is_sure_about_quitting:
self.running = False

def delete_treasure(self):
pass

Expand All @@ -84,7 +91,8 @@ def main(self):
("Make a new treasure", self.new_treasure),
("List current treasures", self.list_treasures),
("Delete a treasure", self.delete_treasure),
("Quit", self.save_and_quit),
("Save and quit", self.save_and_quit),
("Quit without saving", self.quit_without_save)
]
menu_options = [x[0] for x in menu_options_with_actions]
menu_prompt = "Make a choice"
Expand All @@ -98,12 +106,46 @@ def prompt_for_selection(self, prompt, options):
"""Given a list of options and a prompt,
get the users selection and return the index of the selected option
"""
retval = None
# Print out the numbered options
for i, option in enumerate(options):
print "%3s. %s" % (i+1, option)
# Get the users selection
selection = raw_input("%s: " % prompt)
return int(selection)-1
# Continue to prompt user until valid input is recieved.
while retval == None:
# Get the users selection
selection = raw_input("%s: " % prompt)
# Check that the input is valid integer
try:
retval = int(selection) - 1
except ValueError:
print "Invalid input. Please enter a number."
continue
# Ensure input is within the valid range
if retval < 0 or retval >= len(options):
print ("Please enter a number between 1 and %d inclusive."
% len(options))
retval = None # reset the illegal value
continue
return retval

def yes_no_prompt(self, prompt):
'''Prompt for a yes/no answer.
Will accept any response beginning with Y, N, y or n.
Returns a bool.'''
retval = None
selection = raw_input("%s (Y/N): " % prompt)
# Continue to prompt user until valid input starts with "Y" or "N".
while retval == None:
first_letter = selection.strip()[0].upper()
try:
retval = {
"Y": True,
"N": False
}[first_letter]
except KeyError:
pass
return retval


if __name__ == "__main__":
a = Admin()
56 changes: 14 additions & 42 deletions roguey/resources/items.xml
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
<?xml version="1.0" ?>
<items>
<item>
<item_type>
hat
</item_type>
<title>
Hat of Holding
</title>
<description>
There appears to be something in here...
</description>
<item_type>hat</item_type>
<title>Hat of Holding</title>
<description>There appears to be something in here...</description>
</item>
<item>
<item_type>
weapon
</item_type>
<title>
Breadknife of Cleaving
</title>
<description>
Look out -- it's sharp!
</description>
<damage type="int">
10
</damage>
<item_type>weapon</item_type>
<title>Breadknife of Cleaving</title>
<description>Look out -- it's sharp!</description>
<damage type="int">10</damage>
</item>
<item>
<item_type>
pants
</item_type>
<title>
Pants of Breathing
</title>
<description>
No magical powers, but they are made of natural fibre.
</description>
<item_type>pants</item_type>
<title>Pants of Breathing</title>
<description>No magical powers, but they are made of natural fibre.</description>
</item>
<item>
<item_type>
shoes
</item_type>
<title>
Mithral Moccasins
</title>
<description>
Protection _and_ comfort!
</description>
<armor type="int">
3
</armor>
<item_type>shoes</item_type>
<title>Mithral Moccasins</title>
<description>Protection _and_ comfort!</description>
<armor type="int">3</armor>
</item>
</items>

0 comments on commit 6555a2a

Please sign in to comment.