2014-03-26 11 views
-2

有没有办法让代码行在不同的代码行上继续?例如:Python:使代码在不同的行上继续

a = raw_input("1 or 2") 
if a == "1" : 
    a = raw_input("3 or 4") 
    if a == "3" : 
     *line of code that makes the script progress at line #14* 
    if a == "4" : 
     *line of code that makes the script progress at line #14* 
if a = "2" : 
    a = raw_input("5 or 6") 
    if a == "5" : 
     *line of code that makes the script progress at line #14* 
    if a == "6" : 
     *line of code that makes the script progress at line #14* 
print ("chocolate") 
(^line #14^) 
+1

你有更好的例子,更接近你的真实世界的用例吗?这个例子可以简化为'if int(a)<7:print('chocolate')'。 –

回答

-1

您正在寻找的构造是'goto'语句,并且没有python不支持goto语句。看到这个问题:Is there a label/goto in Python?的一些黑客可以用来实现你想要的python类型的延续。

+3

我不认为这是一个有用的答案。很明显,'goto'会尽力做到这一点,但是_当然有办法让代码分支并开始在其他地方执行语句,否则就不会有任何编程。我认为更好地理解OP真正想要的是什么,回答潜在的问题,而不是毫不留情地暗示有更好的方法。 –