2014-03-26 12 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真正想要的是什麼,回答潛在的問題,而不是毫不留情地暗示有更好的方法。 –