[Tutor] Interactive Menu Woes

Evert Rol evert.rol at gmail.com
Wed Nov 14 13:28:45 CET 2007


> I am trying to build a menu for the following script to make it  
> more "user friendly".  Nothing fancy, just a simple add data and  
> look up the entered results.
>
> The problem is that when I run my modified version with this  
> snippet (please see attachment for original and my modified versions):
>
> [code]
> class MenuInput:
>
>  # ask user to input data and store to be passed to manageable  
> variables.
>         def addName(self):
>                 print "To add a name, please input he following  
> information: "
>                 self.enter = phoneentry()
>                 self.name = raw_input("Name: ")
>                 self.number = raw_input("Number: ")
>                 self.get = int(raw_input("What type of number is  
> this? (choose one): \n 1. Home:\n 2. Work:\n 3. Cell:\n : "))
>                 def numberType(self, get = 1):
>                         if self.get == 1:
>                                 self.returnType = HOME
>                                 #return self.getType
>                         elif self.gete == 2:
>                                 self.returnType = WORK
>                                 #return self.getType
>                         elif self.get == 3:
>                                 self.returnType = FAX
>                                 #return self.getType
>                         return self.returnType
>
>                 self.type = numberType(self.get)
>                 self.enter(self.name, self.number, self.returnType)
>


Bypassing the whole 'function within function' problem, consider  
using a dictionary instead: somedict = {1: HOME, 2: WORK, 3: FAX},  
and self.type = somedict[self.get]. This also avoids endless if-elif- 
elif-elif sequences (looking at showtype() in your original script,  
where you can apply the same technique); it's often clearer, and  
certainly shorter.





More information about the Tutor mailing list