python的新功能,菜单选项“返回”

uqjltbpv  于 2023-02-14  发布在  Python
关注(0)|答案(1)|浏览(206)

我绞尽脑汁想这个问题,但我确信这是不必要的。我试着在网上寻找答案,但毫无头绪。
我正在写一个简单的菜单脚本(可能不是最好的格式)。在提示符2、3、4中,我需要一个选项来返回到上一个提示符。基于我所发现的,我只是不断地循环自己进入一个越来越大的问题。
这是我的想法:

print("\nPlease select an action:")
print("\n1. Run")
print("2. Swim")

user_input1 = input("\nPlease make your selection: ")

if user_input1 == "1":
    user_selected_action = "run"
elif user_input1 == "2":
    user_selected_action = "swim"
else:
    print("Invalid option selected. Run script again.")
    exit()

print("\nPlease select an environment:")
print("\n1. Outdoors")
print("2. In a Gym")

user_input2 = input("\nPlease make your selection: ")

if user_input2 == "1":
    user_selected_cluster = 'Outdoors'
elif user_input2 == "2":
    user_selected_cluster = 'Gym'
else:
    print("Invalid option selected. Run script again.")
    exit()

print("\nPlease select an day:")
print("\n1. Saturday")
print("2. Sunday")

user_input3 = input("\nPlease make your selection: ")

if user_input3 == "1":
    user_selected_action = "Sat"
elif user_input3 == "2":
    user_selected_action = "Sun"
else:
    print("Invalid option selected. Run script again.")
    exit()

print("\nPlease select a time of day:")
print("\n1. Day")
print("2. Night")

user_input4 = input("\nPlease make your selection: ")

if user_input4 == "1":
    user_selected_cluster = 'AM'
elif user_input4 == "2":
    user_selected_cluster = 'PM'
else:
    print("Invalid option selected. Run script again.")
    exit()

我尝试过各种while循环,甚至把它们转换成函数,老实说,我还没有完全掌握。
每种解决方案都以循环当前的提示符、循环整个脚本或在prompt_1之后结束脚本而告终
老实说,我更喜欢学习它,而不是有人做它,但我甚至找不到好的视频,解决这个问题。

fslejnso

fslejnso1#

Python(像许多其他语言一样)没有go to选项。
“后藤”语句允许程序流中的无条件跳转,它被认为是一种糟糕的编程实践,因为它会导致混乱和难以维护的代码。相反,Python使用结构化的控制流语句,如if、for和while循环,这些语句鼓励清晰和有组织的代码,易于理解和调试。
但是,您可以将代码放入函数中以实现相同的结果,如下所示:

def main():
    ''' the main module '''

    # have 3 attempts at each question, then move on.
    for i in range(3):
        x = action_01()
        if x != 'invalid': break

    for i in range(3):
        x = action_02()
        if x != 'invalid': break

    for i in range(3):
        x = action_03()
        if x != 'invalid': break

    for i in range(3):
        x = action_04()
        if x != 'invalid': break



def action_01():

    print("\nPlease select an action:")
    print("\n1. Run")
    print("2. Swim")

    user_input1 = input("\nPlease make your selection: ")

    if user_input1 == "1":
        user_selected_action = "run"
    elif user_input1 == "2":
        user_selected_action = "swim"
    else:
        user_selected_action = "invalid"
        print("Invalid option selected. Run script again.")
    return user_selected_action

def action_02():
    print("\nPlease select an environment:")
    print("\n1. Outdoors")
    print("2. In a Gym")

    user_input2 = input("\nPlease make your selection: ")

    if user_input2 == "1":
        user_selected_action = 'Outdoors'
    elif user_input2 == "2":
        user_selected_action = 'Gym'
    else:
        user_selected_action = "invalid"
        print("Invalid option selected. Run script again.")
    return user_selected_action

def action_03():
    print("\nPlease select an day:")
    print("\n1. Saturday")
    print("2. Sunday")

    user_input3 = input("\nPlease make your selection: ")

    if user_input3 == "1":
        user_selected_action = "Sat"
    elif user_input3 == "2":
        user_selected_action = "Sun"
    else:
        user_selected_action = "invalid"
        print("Invalid option selected. Run script again.")
    return user_selected_action

def action_04():
    print("\nPlease select a time of day:")
    print("\n1. Day")
    print("2. Night")

    user_input4 = input("\nPlease make your selection: ")

    if user_input4 == "1":
        user_selected_action = 'AM'
    elif user_input4 == "2":
        user_selected_action = 'PM'
    else:
        user_selected_action = "invalid"
        print("Invalid option selected. Run script again.")
    return user_selected_action
    



if __name__ == '__main__':
    main()

注意,还有更有效的方法,但这将是一个很好的起点。

相关问题