python - 为什么我的 Python 代码从一个函数跳转到另一个我没有调用的函数?

标签 python function

我的程序创建了一个电台列表,其中包含有关它们的一些信息。我设置了三个 edit 函数:edit_mp ()edit_name ()edit_clear ()。这三个只能从 edit_station() 函数开头的菜单中调用。 edit_mp()当前正在接受用户输入,然后跳转到edit_clear()中间。

我有两种类型的对象:一个 Station 对象,它存储一些信息;一个 Subdivision 对象,它存储车站列表以及其中一个功能的排序列表站。

我尝试在函数中添加额外的检查,这表明该函数在“New milepost is”行之后立即停止,并确认 input.upper ()。它应该立即转到 if-else-loop,但是我放入 ifprint 语句不起作用,并且 >else 不会调用 edit_station_menu ()。相反,我收到 “请输入整数” 语句以及来自 edit_clear () 的壁板长度提示。

def edit_station():
    global n
    global mp_edit
    n = 0
    mp_edit = input("Enter milepost of station to edit.")
    while n <= len(sub_list[i].stations):
        if sub_list[i].stations[n].mp == float(mp_edit):
            edit_station_menu()
        elif n < len(sub_list[i].stations):
            n += 1
        else:
            print("Milepost not found.  Enter 1 to try again, or any other key to return.")
            choice = input()
            if choice == "1":
                edit_station()
            else:
                display_sub()

def edit_station_menu():
    global n
    global mp_edit
    sta = sub_list[i].stations[n]
    def edit_mp():
        print("STATION SELECTED: " + sta.name)
        mp = input("Station milepost: ")
        try:
            mp = float(mp)
        except ValueError:
            print("Please enter a valid milepost number (numerals and decimals only).")
            edit_mp()
        else:
            print("New milepost is " + str(mp) + ". Is this correct? Y/N")
            correct = input()
            if correct.upper() == "Y":
                sub_list[i].mp.remove(mp_edit)
                print("Original milepost removed.")
                sta.mp = mp
                sub_list[i].mp.append(sta.mp)
                print("New milepost added.")
                if sub_list[i].ascend_dir == "W" or sub_list[i].ascend_dir == "S":
                    sub_list[i].mp.sort()
                else:
                    sub_list[i].mp.sort()
                    sub_list[i].mp.reverse()
                edit_station_menu()
            else:
                print("Ready to move on, nothing has changed.")
                input()
                edit_mp()

    def edit_name():
        print("STATION SELECTED: " + sta.name)
        name = input("Enter new station name: ").upper()
        print("New station name is " + name + ". Is this correct? Y/N")
        correct = input()
        if correct.upper() == "Y":
            sta.name = name
            edit_station_menu()
        else:
            edit_name()

    def edit_clear():
        def edit_sdglen():
            sdglen = input("Enter siding length: ")
            try:
                sta.sdglen = int(sdglen)
            except ValueError:
                print("Please enter a whole number.")
                edit_sdglen()
            else:
                edit_station_menu()

        def edit_maxtoclear():
            maxtoclear = input("Maximum length that can clear main line at this station: ")
            try:
                sta.clearlen = int(maxtoclear)
            except ValueError:
                print("Please enter a whole number.")
                edit_maxtoclear()
            else:
                sta.canclear = True
                edit_station_menu()

        print("STATION SELECTED: " + sta.name)
        sdg = input("Does this station have a designated passing siding? Y/N")
        if sdg.upper() == "Y":
            edit_sdglen()
        else:
            sta.sdglen = 0
            canclear = input("Can a train clear main track at this station? Y/N")
            if canclear.upper() == "Y":
                edit_maxtoclear()
            else:
                sta.canclear = False
                sta.clearlen = 0
                edit_station_menu()




    print("STATION SELECTED: " + sta.name)
    print("1. Edit MP")
    print("2. Edit name")
    print("3. Edit clearing information")
    print("4. Any other key to return to subdivision")
    choice = input()
    if choice == "1":
        edit_mp()
    elif choice == "2":
        edit_name()
    elif choice == "3":
        edit_clear()
    else:
        display_sub()

我的输出

SOUTHWARD | ALASKA DIVISION                                   |    NORTHWARD
MP        | STATIONS                          | SDG   | CLEAR | CLEAR LENGTH
29.4      | MOOSE PASS                        | 990   | False | 0
18.4      | CROWN POINT                       | 3704  | False | 0
12.0      | DIVIDE                            | 1920  | False | 0
3.4       | SEWARD                            | 0     | True  | 15000
1. Add Station
2. Edit Station
Any other key to save and return.
2
Enter milepost of station to edit.18.4
STATION SELECTED: CROWN POINT
1. Edit MP
2. Edit name
3. Edit clearing information
4. Any other key to return to subdivision
1
STATION SELECTED: CROWN POINT
Station milepost: 25.4
New milepost is 25.4. Is this correct? Y/N
y
Please enter a whole number.
Enter siding length:

首先,程序显示创建的电台列表,然后提示是否添加电台或编辑现有电台。我在Crown Point处输入了错误的里程标,因此我选择编辑,输入已分配给Crown Point的里程标并进行编辑。在询问我这是否正确后,程序应该在提示后执行 if-else 部分,或者 * 更正station中的milepost并在mileposts list中更改自身(排序所必需的) * 或者告诉我我没有改变任何东西并返回菜单。 相反,它会一直向下到 except ValueError 语句上的 edit_clear ()

最佳答案

However, edit_mp() is currently taking user input and then jumping into the middle of the edit_clear() function.

没有。

你已经深入了解递归了。

发生的情况是:edit_station_menu执行edit_clear,edit_clear最终执行edit_station_menu,edit_station_menu执行edit_mp

当 edit_mp 退出时,edit_station_menu 也会退出(因为没有更多事情可做):

        if choice == "1":
            edit_mp()
        elif choice == "2":
            edit_name()
        elif choice == "3":
            edit_clear()
        else:
            display_sub()
        #edit_station_menu exits here because there's nothing more

现在我们得到了第一次执行特定的 edit_station_menu 的时间 - 即“进入 edit_clear() 函数的中间”。

关于python - 为什么我的 Python 代码从一个函数跳转到另一个我没有调用的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062479/

相关文章:

javascript - "new"这个词将函数更改为对象?

python - 将日期转换为月份和年份

python - 如何使用 libclang 的 python 绑定(bind)在 AST 遍历中跳过子树

python - 如何将非结构化数据插入/附加到 bigquery 表

python - 如何使用事件循环和执行程序关闭进程

python - 如何在 Tensorflow 训练期间将 [3751,4] 数据集密集并 reshape 为 [1,6] 数据集

javascript - confirm() 如何在 JavaScript 中工作

PHP默认参数函数调用

c++ - 函数指针是 C++ 中的函数对象吗?

python - 从函数列表中获取随机函数并调用所选函数