Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46841
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Subject | RE: Beginner question |
| Date | 2013-06-04 09:46 +0300 |
| References | <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2622.1370328367.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
That doesn't even works because input() is the same as eval(raw_input()). So 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, 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, which Python don't have.
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 = {
option1: function1,
option2: function2
}
switch.get(randomizer, default)()
Note that switch is a dictionary and you can use it without creating a variable, for example:
{ option1: function1,
option2: function2
}.get(randomizer, default)()
> Date: Mon, 3 Jun 2013 20:39:28 -0700
> Subject: Beginner question
> From: eschneider92@comcast.net
> To: python-list@python.org
>
> Is there a more efficient way of doing this? Any help is gratly appreciated.
>
>
> import random
> def partdeux():
> print('''A man lunges at you with a knife!
> Do you DUCK or PARRY?''')
> option1=('duck')
> option2=('parry')
> optionsindex=[option1, option2]
> randomizer=random.choice(optionsindex)
> while randomizer==option1:
> if input() in option1:
> print('he tumbles over you')
> break
> else:
> print('he stabs you')
> break
> while randomizer==option2:
> if input() in option2:
> print('you trip him up')
> break
> else:
> print('he stabs you')
> break
> partdeux()
> --
> http://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Beginner question eschneider92@comcast.net - 2013-06-03 20:39 -0700
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 09:46 +0300
Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:53 -0700
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 11:17 +0300
Re: Beginner question Anssi Saari <as@sci.fi> - 2013-06-04 10:45 +0300
Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:57 -0700
Re: Beginner question Chris Angelico <rosuav@gmail.com> - 2013-06-04 18:24 +1000
Re: Beginner question Peter Otten <__peter__@web.de> - 2013-06-04 11:23 +0200
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:23 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:35 +0000
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:51 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-07 02:21 +0000
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 12:34 +0100
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:53 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:25 +0000
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:37 +0300
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:47 +0100
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:15 +0100
Re: Beginner question Mitya Sirenef <msirenef@lightbird.net> - 2013-06-04 13:16 -0400
Re: Beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 02:13 -0700
Re: Beginner question Roy Smith <roy@panix.com> - 2013-06-04 09:16 -0400
Re: Beginner question Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:43 +0100
Re: Beginner question eschneider92@comcast.net - 2013-06-05 20:42 -0700
csiph-web