John,
In this case I think using a class is overkill. You can write a simple procedural version like this:
def getOption(): while True: print """ Please choose from the following options. 1) - Normal Unit test with static data. 2) - Normal Unit test with missing data. 3) - Integration test with current DTG. 4) - Integration test with missing data. 5) - Clean directory 6) - Exit """
choice = raw_input("Choice(1-6) ")
try: choice = int(choice) except ValueError: print "Please enter a number from 1 to 6"
if 1 <= choice <= 6: return choice
print "You have entered an invalid entry. Please try again"
option = getOption() print "You chose", option
If you have multiple clients you could parameterize it so it becomes def getOption(prompt, low, high)
I try to do the simplest thing that could possibly work. Sometimes that is just straight line code, with maybe a few functions mixed in. Sometimes a procedural approach works fine. Sometimes classes and objects are the right way to go, but I don't introduce classes until I need them.
Don't get me wrong, I am a huge fan of OOP, but sometimes it is a bigger hammer than you need.
Kent
Ertl, John wrote:
I am trying to do the usual thing of asking for an input and then checking it to see if it is valid. If the entry is not valid then ask again until you get the correct answer.
I have come up with this class. I am trying to make a transition from procedural programming to object oriented. Is this a good approach for such a check? It seems to me this is more work then needed. (I can put in a counter also to break out if you try too many times).
Please comment if you have the time.
class greating:
def __init__(self): self.OK = False self.lowValue = 1 self.highValue = 6
def opening(self): print """ Please choose from the following options. 1) - Normal Unit test with static data. 2) - Normal Unit test with missing data. 3) - Integration test with current DTG. 4) - Integration test with missing data. 5) - Clean directory 6) - Exit """ self.choice = raw_input("Choice(1-6) ")
def check(self):
try:
self.choice = int(self.choice)
except ValueError:
print "Please enter a number from ",self.lowValue," to
",self.highValue
pass
if self.choice > self.highValue or self.choice < self.lowValue:
print "You have entered an invalid entry. Please try again"
else:
self.OK = True
a = greating()
while a.OK != True: a.opening() a.check()
print a.choice
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor