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

时间:2019-09-23 12:29:27

标签: python function

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

我有两种类型的对象:存储一些信息的Station对象和存储站列表以及这些站的一个特征的排序列表的Subdivision对象。

我尝试在函数中添加额外的检查,这表明函数在"New milepost is"行之后停止并确认input.upper ()。它应该立即移动到if-else-loop,但是我放入print的{​​{1}}语句不起作用,并且if不会调用else。相反,我从edit_station_menu ()得到了"Please enter a whole number"语句和壁板长度提示。

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,在已分配给edit的里程碑中进行编辑。在问我这是否正确之后,程序应在提示后执行Crown Point部分,然后执行 *在if-else中更正milepost,并在station中进行更改(需要排序) *或告诉我,我没有改变任何事情,然后返回菜单。 相反,它在list of mileposts语句中一直下降到edit_clear ()

1 个答案:

答案 0 :(得分:0)

  

但是,edit_mp()当前正在接受用户输入,然后跳到edit_clear()函数的中间。

否。

您已经深入到递归了。

会发生什么:edit_station_menu执行edit_clear,最终执行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()函数的中间”。