[Tutor] input problem

James Mills prologic at shortcircuit.net.au
Mon Sep 13 23:16:47 CEST 2010


On Tue, Sep 14, 2010 at 6:45 AM, ANKUR AGGARWAL <coolankur2006 at gmail.com> wrote:
> Suppose i am taking input or various variables like
> a=raw_input("...............") //hello
> b=raw_input("................")//hi
> c=raw_input("...............")//hello
> d=raw_input("..........")//hello
> but i want to run a common function when input is hello
> so instead of
> if a=="hello":
>  fun()
> then again for b and then again for c then d and so on i have to apply the
> code for the specific variable ,
> i want to run the function whenever in the code input is "hello"
> i am wondering is there is any way like
> if input=="hello":
>  fun()
> i hope you get my point.. Help me out please
> Thanks in advance

How about this design pattern:

def hello():
   print "Hello World!"

prompt = "..."
s = raw_input(prompt)
while s:
   if s == "hello":
      hello()
   elif s == "quit":
      break
   s = raw_input(prompt)

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"


More information about the Tutor mailing list