我可以在变量声明中使用条件吗? [英] Can I use a conditional in a variable declaration?

查看:111
本文介绍了我可以在变量声明中使用条件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Scheme中完成了这个,但我不确定我能用Python。


我想要相当于这个:

如果a ==" yes":

answer =" go ahead"

else:

answer =" ;以这种更紧凑的形式停止




a =(如果a ==" yes":" go ahead:" stop" )

Python中有这样的表格吗?我尝试使用lambda

表达式,但我无法让它正常工作。

I''ve done this in Scheme, but I''m not sure I can in Python.

I want the equivalent of this:

if a == "yes":
answer = "go ahead"
else:
answer = "stop"

in this more compact form:
a = (if a == "yes": "go ahead": "stop")
is there such a form in Python? I tried playing around with lambda
expressions, but I couldn''t quite get it to work right.

推荐答案

vo****@gmail.com 写道:
我想要相当于这个:

如果a ==" yes":
answer =" go ahead"
else:
answer =" stop"

以这种更紧凑的形式:

a =(如果a ==" yes":" go ahead":" stop")
I want the equivalent of this:

if a == "yes":
answer = "go ahead"
else:
answer = "stop"

in this more compact form:
a = (if a == "yes": "go ahead": "stop")




如果''true''情况的值永远不会有布尔值

False,你可以使用这个表格:


a =(a ==" yes")和be ahead"或者停止


''和''和''或''的短路评估给出了正确的结果。

这不会工作正确,因为''和''将始终评估为

""这是假的,所以最后一个术语将被评估并返回:


a =(a ==" yes")和""或者停止


和IMO修复它所需的额外语法并不值得麻烦;只需

拼出if / else。


Kent



If the value for the ''true'' case can never have a boolean value of
False, you can use this form:

a = (a == "yes") and "go ahead" or "stop"

The short-circuit evaluation of ''and'' and ''or'' give the correct result.
This will not work correctly because the ''and'' will always evaluate to
"" which is False so the last term will be evaluated and returned:

a = (a == "yes") and "" or "stop"

and IMO the extra syntax needed to fix it isn''t worth the trouble; just
spell out the if / else.

Kent


vo **** @ gmail.com 写道:
我在Scheme中做过这个,但我不是我确定我可以用Python。

我想要相当于这个:

如果a ==" yes":
answer =" go ahead" <另外:
answer =" stop"

这个更紧凑的形式:

a =(如果a ==是":"继续:"停止")

在Python中有这样的表格吗?我尝试使用lambda
表达式,但我无法让它正常工作。
I''ve done this in Scheme, but I''m not sure I can in Python.

I want the equivalent of this:

if a == "yes":
answer = "go ahead"
else:
answer = "stop"

in this more compact form:
a = (if a == "yes": "go ahead": "stop")
is there such a form in Python? I tried playing around with lambda
expressions, but I couldn''t quite get it to work right.




将在Python 2.5(最终版本)计划于2006年8月发布):



There will be, in Python 2.5 (final release scheduled for August 2006):

answer =" go ahead"如果a ==是其他停止
answer = "go ahead" if a=="yes" else "stop"




请参阅:
http://mail.python.org/pipermail/pyt...er/056846.html
http://www.python.org/doc / peps / pep-0308 /

--Ben



See:
http://mail.python.org/pipermail/pyt...er/056846.html
http://www.python.org/doc/peps/pep-0308/

--Ben


肯特 - 谢谢快速回复。我尝试了和/或技巧 - 它确实是
工作。但你是对的 - 比它的价值更麻烦......所以,就目前而言,我已经做了b $ b做了很长的事情。它看起来像(见下文),这个功能

将很快添加。


感谢您的快速帮助。


-sam

Kent - Thanks for the quick reply. I tried the and/or trick - it does
work. But you''re right - more trouble than its worth.... So for now, I
did it "the long way". It looks like (see below), this functionality
will be added in soon.

Thanks for the quick help.

-sam


这篇关于我可以在变量声明中使用条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆