Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'beginner': 0.05; "'default'": 0.09; 'clause': 0.09; 'emulate': 0.09; 'exception.': 0.09; 'variable,': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'email addr:comcast.net': 0.16; 'loop.': 0.16; 'skip:} 10': 0.16; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'creating': 0.23; 'this?': 0.23; 'received:65.55.116': 0.24; 'mon,': 0.24; 'question': 0.24; '>': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'appreciated.': 0.29; "doesn't": 0.30; 'url:mailman': 0.30; 'that.': 0.31; 'url:python': 0.33; 'date:': 0.34; 'there': 0.35; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'subject:': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'even': 0.60; 'break': 0.61; "you'll": 0.62; 'more': 0.64; 'email name:python-list': 0.65; 'statement,': 0.68; 'skip:r 30': 0.69; 'have.': 0.93; '2013': 0.98 X-TMN: [IMfegvpk5RdxuNKx8lS8IS04mL6LJG9G] X-Originating-Email: [carlosnepomuceno@outlook.com] Content-Type: multipart/alternative; boundary="_bf0825c6-c3df-40af-aca8-dcd6ff5f0551_" From: Carlos Nepomuceno To: "python-list@python.org" Subject: RE: Beginner question Date: Tue, 4 Jun 2013 09:46:03 +0300 Importance: Normal In-Reply-To: <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> References: <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> MIME-Version: 1.0 X-OriginalArrivalTime: 04 Jun 2013 06:46:04.0120 (UTC) FILETIME=[2EEF2D80:01CE60EF] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 139 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370328367 news.xs4all.nl 15888 [2001:888:2000:d::a6]:40212 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46841 --_bf0825c6-c3df-40af-aca8-dcd6ff5f0551_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable That doesn't even works because input() is the same as eval(raw_input()). S= o you'll get a NameError exception. I think you know that. Perhaps you mean raw_input() instead of input(). In that case the answer is yes=2C it can be more 'efficient' because the if= -then-else clause always breaks the while loop. I think you are looking for is a switch statement=2C which Python don't hav= e. You can use the following structure to emulate a switch statement: def function1(): if raw_input() in option1: print('he tumbles over you') else: print('he stabs you') def function2(): if raw_input() in option2: print('you trip him up') else: print('he stabs you') def default(): print 'DEFAULT' switch =3D { option1: function1=2C option2: function2 } switch.get(randomizer=2C default)() Note that switch is a dictionary and you can use it without creating a vari= able=2C for example: { option1: function1=2C option2: function2 }.get(randomizer=2C default)() > Date: Mon=2C 3 Jun 2013 20:39:28 -0700 > Subject: Beginner question > From: eschneider92@comcast.net > To: python-list@python.org >=20 > Is there a more efficient way of doing this? Any help is gratly appreciat= ed. >=20 >=20 > import random > def partdeux(): > print('''A man lunges at you with a knife! > Do you DUCK or PARRY?''') > option1=3D('duck') > option2=3D('parry') > optionsindex=3D[option1=2C option2] > randomizer=3Drandom.choice(optionsindex) > while randomizer=3D=3Doption1: > if input() in option1: > print('he tumbles over you') > break > else: > print('he stabs you') > break > while randomizer=3D=3Doption2: > if input() in option2: > print('you trip him up') > break > else: > print('he stabs you') > break > partdeux() > --=20 > http://mail.python.org/mailman/listinfo/python-list = --_bf0825c6-c3df-40af-aca8-dcd6ff5f0551_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
That doesn't even works because = input() is the same as eval(raw_input()). So you'll get a NameError excepti= on.

I think you know that. Perhaps you mean raw_input() instead of i= nput().
In that case the answer is yes=2C it can be more 'efficient' bec= ause the if-then-else clause always breaks the while loop.
I think you a= re looking for is a switch statement=2C which Python don't have.

You= can use the following structure to emulate a switch statement:

def = function1():
 =3B =3B =3B if raw_input() in option1:
&nbs= p=3B =3B =3B =3B =3B =3B =3B print('he tumbles over= you')
 =3B =3B =3B else:
 =3B =3B =3B = =3B =3B =3B =3B print('he stabs you')

def function2(): =3B =3B =3B if raw_input() in option2:
 =3B =3B&n= bsp=3B =3B =3B =3B =3B print('you trip him up')
 =3B=  =3B =3B else:
 =3B =3B =3B =3B =3B =3B&= nbsp=3B print('he stabs you')

def default():
 =3B =3B&nbs= p=3B print 'DEFAULT'

switch =3D {
 =3B =3B =3B option= 1: function1=2C
 =3B =3B =3B option2: function2
}
swit= ch.get(randomizer=2C default)()

Note that switch is a dictionary and= you can use it without creating a variable=2C for example:

{ = =3B =3B option1: function1=2C
 =3B =3B =3B option2: func= tion2
}.get(randomizer=2C default)()


>=3B Date: Mon=2C= 3 Jun 2013 20:39:28 -0700
>=3B Subject: Beginner question
>=3B F= rom: eschneider92@comcast.net
>=3B To: python-list@python.org
>= =3B
>=3B Is there a more efficient way of doing this? Any help is gra= tly appreciated.
>=3B
>=3B
>=3B import random
>=3B de= f partdeux():
>=3B print('''A man lunges at you with a knife!
&= gt=3B Do you DUCK or PARRY?''')
>=3B option1=3D('duck')
>=3B = option2=3D('parry')
>=3B optionsindex=3D[option1=2C option2]>=3B randomizer=3Drandom.choice(optionsindex)
>=3B while r= andomizer=3D=3Doption1:
>=3B if input() in option1:
>=3B = print('he tumbles over you')
>=3B break
>= =3B else:
>=3B print('he stabs you')
>=3B = break
>=3B while randomizer=3D=3Doption2:
>=3B = if input() in option2:
>=3B print('you trip him up')>=3B break
>=3B else:
>=3B pri= nt('he stabs you')
>=3B break
>=3B partdeux()
>= =3B --
>=3B http://mail.python.org/mailman/listinfo/python-list
= --_bf0825c6-c3df-40af-aca8-dcd6ff5f0551_--