Python返回到另一行代码

Python返回到另一行代码,python,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

有没有一种方法可以在多个输入之后沿着不同的路径运行,使其在另一行代码上继续运行,而不管遵循的是哪条路径? 例如:

    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^)

您需要的是一个转到语句。幸运的是,Python没有提供此功能。您可以尝试改用函数:

def print_chocolate():
    print("chocolate")

if a == "1" :
    a = raw_input("3 or 4")
    if a == "3" or a == "4" :
        print_chocolate()

if a == "2" :
    a = raw_input("5 or 6")
    if a == "5" or a == "6":
        print_chocolate()
注意:


您可以使用逻辑运算符(
)保存一些代码行。

您要查找的是一个转到语句。幸运的是,Python没有提供此功能。您可以尝试改用函数:

def print_chocolate():
    print("chocolate")

if a == "1" :
    a = raw_input("3 or 4")
    if a == "3" or a == "4" :
        print_chocolate()

if a == "2" :
    a = raw_input("5 or 6")
    if a == "5" or a == "6":
        print_chocolate()
注意:


您可以使用逻辑运算符(
)保存一些代码行。

您要查找的是一个转到语句。幸运的是,Python没有提供此功能。您可以尝试改用函数:

def print_chocolate():
    print("chocolate")

if a == "1" :
    a = raw_input("3 or 4")
    if a == "3" or a == "4" :
        print_chocolate()

if a == "2" :
    a = raw_input("5 or 6")
    if a == "5" or a == "6":
        print_chocolate()
注意:


您可以使用逻辑运算符(
)保存一些代码行。

您要查找的是一个转到语句。幸运的是,Python没有提供此功能。您可以尝试改用函数:

def print_chocolate():
    print("chocolate")

if a == "1" :
    a = raw_input("3 or 4")
    if a == "3" or a == "4" :
        print_chocolate()

if a == "2" :
    a = raw_input("5 or 6")
    if a == "5" or a == "6":
        print_chocolate()
注意:


您可以通过使用逻辑运算符(
)来保存一些代码行。

想要使用goto通常是一个指示,看看您是否可以更好地构造代码:我同意,有了良好的控制结构,您不应该需要goto(关于这一点的许多讨论参见例如)

在我看来,你的计划应该反映你的意图并明确划分责任。我想这是一个很有趣的例子,但似乎仍有两件事在进行:获取和解释用户输入,以及输出。我认为一个更清晰的程序,可以避免跳槽,如下所示

def UserInputDeservesChocolate():
    chocolate = False
    a = raw_input("1 or 2")
    if a == "1" :
        a = raw_input("3 or 4")
        chocolate = a == "3" or a == "4"
    if a == "2" :
        a = raw_input("5 or 6")
        chocolate = a == "5" or a == "6"
    return chocolate

if UserInputDeservesChocolate():
    print ("chocolate")

想要使用goto通常是一种迹象,表明您是否可以更好地构造代码:我同意,有了良好的控制结构,您不应该需要goto(关于这方面的许多讨论,请参阅例如)

在我看来,你的计划应该反映你的意图并明确划分责任。我想这是一个很有趣的例子,但似乎仍有两件事在进行:获取和解释用户输入,以及输出。我认为一个更清晰的程序,可以避免跳槽,如下所示

def UserInputDeservesChocolate():
    chocolate = False
    a = raw_input("1 or 2")
    if a == "1" :
        a = raw_input("3 or 4")
        chocolate = a == "3" or a == "4"
    if a == "2" :
        a = raw_input("5 or 6")
        chocolate = a == "5" or a == "6"
    return chocolate

if UserInputDeservesChocolate():
    print ("chocolate")

想要使用goto通常是一种迹象,表明您是否可以更好地构造代码:我同意,有了良好的控制结构,您不应该需要goto(关于这方面的许多讨论,请参阅例如)

在我看来,你的计划应该反映你的意图并明确划分责任。我想这是一个很有趣的例子,但似乎仍有两件事在进行:获取和解释用户输入,以及输出。我认为一个更清晰的程序,可以避免跳槽,如下所示

def UserInputDeservesChocolate():
    chocolate = False
    a = raw_input("1 or 2")
    if a == "1" :
        a = raw_input("3 or 4")
        chocolate = a == "3" or a == "4"
    if a == "2" :
        a = raw_input("5 or 6")
        chocolate = a == "5" or a == "6"
    return chocolate

if UserInputDeservesChocolate():
    print ("chocolate")

想要使用goto通常是一种迹象,表明您是否可以更好地构造代码:我同意,有了良好的控制结构,您不应该需要goto(关于这方面的许多讨论,请参阅例如)

在我看来,你的计划应该反映你的意图并明确划分责任。我想这是一个很有趣的例子,但似乎仍有两件事在进行:获取和解释用户输入,以及输出。我认为一个更清晰的程序,可以避免跳槽,如下所示

def UserInputDeservesChocolate():
    chocolate = False
    a = raw_input("1 or 2")
    if a == "1" :
        a = raw_input("3 or 4")
        chocolate = a == "3" or a == "4"
    if a == "2" :
        a = raw_input("5 or 6")
        chocolate = a == "5" or a == "6"
    return chocolate

if UserInputDeservesChocolate():
    print ("chocolate")

我认为这意味着糟糕的程序设计。。。你可能想重新考虑你的控制流程,我认为这意味着程序设计很差。。。你可能想重新考虑你的控制流程,我认为这意味着程序设计很差。。。你可能想重新考虑你的控制流程,我认为这意味着程序设计很差。。。您可能希望“不幸地”->“幸运地”->“不幸地”->“幸运地”->“不幸地”->“幸运地”->“幸运地”->“不幸地”-->“幸运地”