好的python相当于C goto [英] Good python equivalent to C goto
问题描述
你好,
对于以下C代码的一个好的python等价的任何建议:
while(loopCondition)
{
if(condition1)
goto next;
if(condition2)
转到下一个;
if(condition3)
转到下一个;
stmt1;
stmt2;
下一个:
stmt3;
stmt4;
}
谢谢
Kurien
Hello,
Any suggestions on a good python equivalent for the following C code:
while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}
Thanks
Kurien
推荐答案
Kurien Mathew schrieb:
Kurien Mathew schrieb:
有关a的任何建议以下C代码相当于以下好的python:
while(loopCondition)
{
if(condition1)
转到下一个;
if(condition2)
转到下一个;
if(condition3)
转到下一个;
stmt1;
stmt2;
下一个:
stmt3;
stmt4;
}
Any suggestions on a good python equivalent for the following C code:
while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}
while loopCondition:
如果不是(cond1或cond2或cond3):
stmt1
stmt2
stmt3
stmt4
未经测试。
问候,
Thomas
-
Ce n''est pas parce qu''ils sontnombreuxàavoirtort qu''ils ont raison!
(Coluche)
while loopCondition:
if not( cond1 or cond2 or cond3 ):
stmt1
stmt2
stmt3
stmt4
Not tested.
Greetings,
Thomas
--
Ce n''est pas parce qu''ils sont nombreux à avoir tort qu''ils ont raison!
(Coluche)
Kurien Mathew写道:
Kurien Mathew wrote:
关于一个好的python的任何建议等效于以下C代码:
while(loopCondition)
{
if(condition1)
转到下一个;
if(condition2)
转到下一个;
if(condition3)
转到下一个;
stmt1;
stmt2;
下一个:
stmt3;
stmt4 ;
}
Any suggestions on a good python equivalent for the following C code:
while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}
好像你*不想*执行第一部分(如果有的话)
条件是真的。换句话说,
而loopCondition:
如果不是(condition1或condition2或condition3):
stmt1
stmt2
stmt3
stmt4
< / F>
seems as if you *don''t* want to execute the first part if any of the
conditions are true. in other words,
while loopCondition:
if not (condition1 or condition2 or condition3):
stmt1
stmt2
stmt3
stmt4
</F>
Kurien Mathew写道:
Kurien Mathew wrote:
你好,
关于一个好的python等价物的任何建议对于以下C代码:
Hello,
Any suggestions on a good python equivalent for the following C code:
有多种方法可以用Python编写示例。例如
while loopCondition:
condition = 1
条件:
如果条件1:
休息
如果条件2:
休息
如果条件3:
休息
stmt1
stmt2
条件= 0
否则:
stmt3 >
stmt4
如果你突然爆发的话,其余的阻止是不会执行的。
你可以效仿多个gotos有例外。一般来说,你不应该尝试在Python代码中模仿C。像Python代码这样的C代码通常难以读取,而且比设计良好的Python代码慢。
There are various ways to write your example in Python. For example
while loopCondition:
condition = 1
while condition:
if condition1:
break
if condition2:
break
if condition3:
break
stmt1
stmt2
condition = 0
else:
stmt3
stmt4
The else block of while isn''t execute if you break out of while.
You can emulate multiple gotos with exceptions. In general you shouldn''t
try to mimic C in Python code. C like Python code is often hard to read
and slower than well designed Python code.
这篇关于好的python相当于C goto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!