Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46831 > unrolled thread
| Started by | eschneider92@comcast.net |
|---|---|
| First post | 2013-06-03 20:39 -0700 |
| Last post | 2013-06-05 20:42 -0700 |
| Articles | 20 on this page of 23 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 1 of 2 [1] 2 Next page →
| From | eschneider92@comcast.net |
|---|---|
| Date | 2013-06-03 20:39 -0700 |
| Subject | Beginner question |
| Message-ID | <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> |
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()
[toc] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 09:46 +0300 |
| Message-ID | <mailman.2622.1370328367.3114.python-list@python.org> |
| In reply to | #46831 |
[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
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <john_ladasky@sbcglobal.net> |
|---|---|
| Date | 2013-06-04 00:53 -0700 |
| Message-ID | <48f73b41-9ff0-4fc0-a23b-f3eba8d02c80@googlegroups.com> |
| In reply to | #46841 |
On Monday, June 3, 2013 11:46:03 PM UTC-7, Carlos Nepomuceno wrote: > 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(). But the OP's code shows print() functions... which is not the habit of Python 2 programmers, even though it's legal code. And the OP says s/he's a beginning programmer... so why start learning Python 2 in 2013? Let me ask the OP, are you programming in Python 2 or Python 3? If the answer is indeed Python 3: raw_input() has been banished from Python 3, in favor of plain-old input().
[toc] | [prev] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 11:17 +0300 |
| Message-ID | <mailman.2627.1370333931.3114.python-list@python.org> |
| In reply to | #46851 |
[Multipart message — attachments visible in raw view] — view raw
> Date: Tue, 4 Jun 2013 00:53:04 -0700 > Subject: Re: Beginner question > From: john_ladasky@sbcglobal.net > To: python-list@python.org > > On Monday, June 3, 2013 11:46:03 PM UTC-7, Carlos Nepomuceno wrote: > > 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(). > > But the OP's code shows print() functions... which is not the habit of Python 2 programmers, even though it's legal code. And the OP says s/he's a beginning programmer... so why start learning Python 2 in 2013? Let me ask the OP, are you programming in Python 2 or Python 3? > > If the answer is indeed Python 3: raw_input() has been banished from Python 3, in favor of plain-old input(). Didn't know that. Thanks! > -- > http://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2013-06-04 10:45 +0300 |
| Message-ID | <vg3hahefhel.fsf@coffee.modeemi.fi> |
| In reply to | #46831 |
eschneider92@comcast.net writes: > Is there a more efficient way of doing this? Any help is gratly appreciated. Efficiency in a short program isn't a big thing. You have some pretty weird things in there, there's no need make single element tuples out of your strings and then putting those in a list. Just put the strings in a tuple and go. Likewise there's really no point in having while loops where you exit on the first round now is there? Just use an if. BTW, did I get the logic correctly, the end result is random? If true then the logic can be simplified greatly, you can just discard the user input and print a random choice of your three result strings...
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <john_ladasky@sbcglobal.net> |
|---|---|
| Date | 2013-06-04 00:57 -0700 |
| Message-ID | <1ba2ceec-f778-4c95-bf98-260fc48b4c3f@googlegroups.com> |
| In reply to | #46849 |
On Tuesday, June 4, 2013 12:45:38 AM UTC-7, Anssi Saari wrote: > BTW, did I get the logic correctly, the end result is random? You're right! I'm guessing that's not what the OP wants?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-06-04 18:24 +1000 |
| Message-ID | <mailman.2628.1370334257.3114.python-list@python.org> |
| In reply to | #46852 |
On Tue, Jun 4, 2013 at 5:57 PM, John Ladasky <john_ladasky@sbcglobal.net> wrote: > On Tuesday, June 4, 2013 12:45:38 AM UTC-7, Anssi Saari wrote: > >> BTW, did I get the logic correctly, the end result is random? > > You're right! I'm guessing that's not what the OP wants? I'm guessing that's exactly what the OP wants. This is a fairly classic programming puzzle; on the surface it appears that you have some influence on the outcome, but ultimately you're playing rock-paper-scissors with the Random Number God. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-04 11:23 +0200 |
| Message-ID | <mailman.2629.1370337801.3114.python-list@python.org> |
| In reply to | #46852 |
Chris Angelico wrote:
> On Tue, Jun 4, 2013 at 5:57 PM, John Ladasky <john_ladasky@sbcglobal.net>
> wrote:
>> On Tuesday, June 4, 2013 12:45:38 AM UTC-7, Anssi Saari wrote:
>>
>>> BTW, did I get the logic correctly, the end result is random?
>>
>> You're right! I'm guessing that's not what the OP wants?
>
> I'm guessing that's exactly what the OP wants. This is a fairly
> classic programming puzzle; on the surface it appears that you have
> some influence on the outcome, but ultimately you're playing
> rock-paper-scissors with the Random Number God.
As it is written, don't you always win if you hit enter?
It may be the approved cheat code, though...
OP:
("some string")
is not a tuple, it is the same as just
"some string"
therefore
option1 = "some string"
if input() in option1:
print("yes")
prints 'yes' if the user types in a substring of option1, and the shortest
substring of any string is "".
For a single-item tuple the trailing comma is mandatory:
>>> ("some string") # string
'some string'
>>> "some string", # tuple
('some string',)
>>> ("some string",) # tuple, parens added for clarity
('some string',)
In general a tuple is consituted by the comma(s), not the parentheses:
>>> "one", "two"
('one', 'two')
[toc] | [prev] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 14:23 +0300 |
| Message-ID | <mailman.2632.1370345087.3114.python-list@python.org> |
| In reply to | #46852 |
[Multipart message — attachments visible in raw view] — view raw
Started answering... now I'm asking! lol
I've tried to use dict() to create a dictionary to use like the switch statement providing variable names instead of literals, such as:
>>> a='A'
>>> b='B'
>>> {a:0,b:1} #here the variables are resolved
{'A': 0, 'B': 1}
That's ok! But if I use dict() declaration:
>>> dict(a=0,b=1)
{'a': 0, 'b': 1} #here variable names are taken as literals
What's going on? Is there a way to make dict() to resolve the variables?
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-06-04 12:35 +0000 |
| Message-ID | <51addf2f$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #46871 |
On Tue, 04 Jun 2013 14:23:39 +0300, Carlos Nepomuceno wrote:
> Started answering... now I'm asking! lol
>
> I've tried to use dict() to create a dictionary to use like the switch
> statement providing variable names instead of literals, such as:
>
>>>> a='A'
>>>> b='B'
>>>> {a:0,b:1} #here the variables are resolved
> {'A': 0, 'B': 1}
>
> That's ok! But if I use dict() declaration:
>
>>>> dict(a=0,b=1)
> {'a': 0, 'b': 1} #here variable names are taken as literals
>
> What's going on? Is there a way to make dict() to resolve the variables?
This is by design. You're calling a function, dict(), and like all
functions, code like:
func(name=value)
provides a *keyword argument*, where the argument is called "name" and
the argument's value is as given. dict is no different from any other
function, it has no superpowers, keyword arguments are still keyword
arguments.
In this case, there is no simple way to use the dict() function[1] the
way you want. You could build up a string and then call eval():
s = "dict(%s=0, %s=1)" % (a, b)
d = eval(s)
but that's slow and inconvenient and dangerous if your data is untrusted.
So in this specific case, you should stick to the {} method.
[1] Technically it's a type, not a function, but the difference makes no
difference here.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 15:51 +0300 |
| Message-ID | <mailman.2644.1370350307.3114.python-list@python.org> |
| In reply to | #46887 |
[Multipart message — attachments visible in raw view] — view raw
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Beginner question
> Date: Tue, 4 Jun 2013 12:35:59 +0000
> To: python-list@python.org
>
> On Tue, 04 Jun 2013 14:23:39 +0300, Carlos Nepomuceno wrote:
>
> > Started answering... now I'm asking! lol
> >
> > I've tried to use dict() to create a dictionary to use like the switch
> > statement providing variable names instead of literals, such as:
> >
> >>>> a='A'
> >>>> b='B'
> >>>> {a:0,b:1} #here the variables are resolved
> > {'A': 0, 'B': 1}
> >
> > That's ok! But if I use dict() declaration:
> >
> >>>> dict(a=0,b=1)
> > {'a': 0, 'b': 1} #here variable names are taken as literals
> >
> > What's going on? Is there a way to make dict() to resolve the variables?
>
>
> This is by design. You're calling a function, dict(), and like all
> functions, code like:
>
> func(name=value)
>
> provides a *keyword argument*, where the argument is called "name" and
> the argument's value is as given. dict is no different from any other
> function, it has no superpowers, keyword arguments are still keyword
> arguments.
>
> In this case, there is no simple way to use the dict() function[1] the
> way you want. You could build up a string and then call eval():
>
> s = "dict(%s=0, %s=1)" % (a, b)
> d = eval(s)
>
> but that's slow and inconvenient and dangerous if your data is untrusted.
>
> So in this specific case, you should stick to the {} method.
>
>
>
> [1] Technically it's a type, not a function, but the difference makes no
> difference here.
>
> --
> Steven
It's superclear now! You're an excelent teacher!
Can you explain me the difference of the type and function you've just mentioned?
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-06-07 02:21 +0000 |
| Message-ID | <51b143a2$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #46890 |
Sorry for the delay in replying.
On Tue, 04 Jun 2013 15:51:38 +0300, Carlos Nepomuceno wrote:
>> [1] Technically it's a type, not a function, but the difference makes
>> no difference here.
> Can you explain me the difference of the type and function you've just
> mentioned?
We were talking about dict().
In Python, "type" is another name for "class". There is a built-in class
called "dict":
py> dict
<class 'dict'>
The way we create a new instance of a class is to call it, as if it were
a function:
py> dict()
{}
just like you might call some other function:
py> len([])
0
so sometimes it is convenient to be lazy and just refer to the type/class
as a function. The general term for things which can be called in Python
is "callable", which includes functions, methods, and types.
(Back in the ancient days of Python 1.x, dict *actually was a function*,
just like len() or ord(), and the type/class system was radically
different. But that's ancient history now.)
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-04 12:34 +0100 |
| Message-ID | <mailman.2636.1370345675.3114.python-list@python.org> |
| In reply to | #46852 |
[Multipart message — attachments visible in raw view] — view raw
On 4 Jun 2013 12:28, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
>
> Started answering... now I'm asking! lol
>
> I've tried to use dict() to create a dictionary to use like the switch
statement providing variable names instead of literals, such as:
>
> >>> a='A'
> >>> b='B'
> >>> {a:0,b:1} #here the variables are resolved
> {'A': 0, 'B': 1}
>
> That's ok! But if I use dict() declaration:
>
> >>> dict(a=0,b=1)
> {'a': 0, 'b': 1} #here variable names are taken as literals
>
> What's going on? Is there a way to make dict() to resolve the variables?
Well yes.
dict(**{a:0,b:1})
The dict() constructor makes a dictionary from keyword arguments. So you
just have to feed it keyword arguments using **.
And if you're in a bad day,
dict(**locals())
[toc] | [prev] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 14:53 +0300 |
| Message-ID | <mailman.2638.1370346812.3114.python-list@python.org> |
| In reply to | #46852 |
[Multipart message — attachments visible in raw view] — view raw
>On 4 Jun 2013 12:28, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com> wrote:
[...]
>> What's going on? Is there a way to make dict() to resolve the variables?
>Well yes.
>dict(**{a:0,b:1})
>The dict() constructor makes a dictionary from keyword arguments. So you just have to feed it keyword arguments using **.
>And if you're in a bad day,
>dict(**locals())
That's exactly the same!
>>>dict(**{a:0,b:1})=={a:0,b:1}
True
Are there any benefits from using dict() instead of {}?
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-06-04 12:25 +0000 |
| Message-ID | <51addcb7$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #46880 |
On Tue, 04 Jun 2013 14:53:29 +0300, Carlos Nepomuceno wrote:
> That's exactly the same!
>>>>dict(**{a:0,b:1})=={a:0,b:1}
> True
Of course it is. Isn't that what you wanted?
It's also a waste of time, because you create a dict literal using {},
then unpack it into keyword arguments, then call dict() to create a new
dict with the same content. Rather like doing this:
n = int(str(42))
only even more expensive.
> Are there any benefits from using dict() instead of {}?
Of course there are. {} can be used for creating dict literals, which
means you are limited to key/values that you explicitly include. dict(),
on the other hand, has a rich set of constructor APIs:
py> help(dict)
Help on class dict in module builtins:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
py> dict(zip('abcd', range(4)), x=23, y=42, z=999)
{'a': 0, 'c': 2, 'b': 1, 'd': 3, 'y': 42, 'x': 23, 'z': 999}
Try doing that with {} alone!
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Date | 2013-06-04 15:37 +0300 |
| Message-ID | <mailman.2642.1370349451.3114.python-list@python.org> |
| In reply to | #46886 |
[Multipart message — attachments visible in raw view] — view raw
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Beginner question
> Date: Tue, 4 Jun 2013 12:25:27 +0000
> To: python-list@python.org
>
> On Tue, 04 Jun 2013 14:53:29 +0300, Carlos Nepomuceno wrote:
>
> > That's exactly the same!
> >>>>dict(**{a:0,b:1})=={a:0,b:1}
> > True
>
>
> Of course it is. Isn't that what you wanted?
Indeed! But that form isn't economically viable as you noted.
>
> It's also a waste of time, because you create a dict literal using {},
> then unpack it into keyword arguments, then call dict() to create a new
> dict with the same content. Rather like doing this:
>
> n = int(str(42))
>
> only even more expensive.
>
>
> > Are there any benefits from using dict() instead of {}?
>
> Of course there are. {} can be used for creating dict literals, which
> means you are limited to key/values that you explicitly include. dict(),
> on the other hand, has a rich set of constructor APIs:
>
> py> help(dict)
>
> Help on class dict in module builtins:
>
> class dict(object)
> | dict() -> new empty dictionary
> | dict(mapping) -> new dictionary initialized from a mapping object's
> | (key, value) pairs
> | dict(iterable) -> new dictionary initialized as if via:
> | d = {}
> | for k, v in iterable:
> | d[k] = v
> | dict(**kwargs) -> new dictionary initialized with the name=value pairs
> | in the keyword argument list. For example: dict(one=1, two=2)
>
>
> py> dict(zip('abcd', range(4)), x=23, y=42, z=999)
> {'a': 0, 'c': 2, 'b': 1, 'd': 3, 'y': 42, 'x': 23, 'z': 999}
Awesome! Now I can do it just like that:
>>> dict([(chr(ord('a')+x),x) for x in range(2)])
{'a': 0, 'b': 1}
Thanks a lot! ;)
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-04 13:47 +0100 |
| Message-ID | <mailman.2643.1370350066.3114.python-list@python.org> |
| In reply to | #46886 |
[Multipart message — attachments visible in raw view] — view raw
>
> Awesome! Now I can do it just like that:
>
> >>> dict([(chr(ord('a')+x),x) for x in range(2)])
> {'a': 0, 'b': 1}
>
> Thanks a lot! ;)
>
Or
dict((c, i) for (i, c) in enumerate('ab'))
But at this point you could just use a dict comprehension.
{c: i for i, c in enumerate('ab')}
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-04 13:15 +0100 |
| Message-ID | <mailman.2641.1370348127.3114.python-list@python.org> |
| In reply to | #46852 |
[Multipart message — attachments visible in raw view] — view raw
On 4 Jun 2013 12:57, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
>
> >On 4 Jun 2013 12:28, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
> [...]
>
> >> What's going on? Is there a way to make dict() to resolve the
variables?
> >Well yes.
> >dict(**{a:0,b:1})
> >The dict() constructor makes a dictionary from keyword arguments. So you
just have to feed it keyword arguments using **.
> >And if you're in a bad day,
> >dict(**locals())
>
> That's exactly the same!
> >>>dict(**{a:0,b:1})=={a:0,b:1}
> True
>
> Are there any benefits from using dict() instead of {}?
Other than being able to create a dict from a list of tuples, and copying a
dict using dict(anotherdict.items()), not that I know of.
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-06-04 13:16 -0400 |
| Message-ID | <mailman.2667.1370366201.3114.python-list@python.org> |
| In reply to | #46852 |
On 06/04/2013 07:53 AM, Carlos Nepomuceno wrote:
> >On 4 Jun 2013 12:28, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com> wrote:
> [...]
> >> What's going on? Is there a way to make dict() to resolve the
variables?
> >Well yes.
> >dict(**{a:0,b:1})
> >The dict() constructor makes a dictionary from keyword arguments. So
you just have to feed it keyword arguments using **.
> >And if you're in a bad day,
> >dict(**locals())
>
> That's exactly the same!
> >>>dict(**{a:0,b:1})=={a:0,b:1}
> True
>
> Are there any benefits from using dict() instead of {}?
>
Other than what Steven already mentioned, a big advantage is that it's
easier to make a dict if you have a lot of keys that are valid
identifiers, and it's more readable, to boot:
dict(red=1, blue=2, orange=3, violet=4, crimson=5, ...)
VS.
{'red':1, 'blue':2, 'orange':3, 'violet':4, 'crimson':5, ...}
-m
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
Although the most acute judges of the witches and even the witches
themselves, were convinced of the guilt of witchery, the guilt nevertheless
was non-existent. It is thus with all guilt. Friedrich Nietzsche
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2013-06-04 02:13 -0700 |
| Message-ID | <IKKdneVcX7wyMjDMnZ2dnUVZ_sSdnZ2d@giganews.com> |
| In reply to | #46831 |
On 06/03/2013 08:39 PM, eschneider92@comcast.net wrote:
> 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()
>
Yes, you are making this much more complicated than necessary. It seems that what you are
trying to do is input the option, then randomly print a success or failure message for that
option. I suspect you didn't plan it, but it also prints the "stab" message for invalid entries.
Like any program, it can be approached in many different ways. Here is one possibility.
(Just the function def, add the import and function call as well. Also I am not adding any
comments. See if you can follow the logic here yourself.)
----------
def partdeux():
print('A man lunges at you with a knife!')
option = input('Do you DUCK or PARRY? ').lower()
success = random.randint(0, 1)
if success:
if option == 'duck':
print('He tumbles over you')
return
if option == 'parry':
print('You trip him up')
return
print('He stabs you')
------------
BTW, ignore the response from Carlos. I can see from the print() functions in your original
that you're using Python 3. His answer is only valid for Python 2.
-=- Larry -=-
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web