Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46841

RE: Beginner question

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 <carlosnepomuceno@outlook.com>
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; '&gt;': 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 <carlosnepomuceno@outlook.com>
To "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2622.1370328367.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


[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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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