为什么我的程序会调出不同的代码?

时间:2018-12-26 12:08:38

标签: python-3.x

所以我正在为我的python类编写作业(这是我的第一个大型作业,男孩,这太难了。)

因此,如果我运行下面的代码(这只是我的作业的一部分,我没有复制粘贴我的作业中的整个代码,以便于大家使用,我还尝试尽可能地缩短代码) xD)。如果我输入我的名字,然后输入10(仅当我输入10表示时间,L表示目的地)和L表示目的地时,代码将按预期工作并打印出票证并将1替换为0名单。但是,当我问是否要继续运行该程序时,当我键入Yes时,它将从下面的方式中调用else:代码,但我不知道为什么会这样做,所以我希望你们可以提供帮助我出xD

提前致谢,如果无法正确表达此问题,我深表歉意

编辑:我想我应该给你们一个关于这项任务的想法。因此,我要创建一个程序,该程序将根据轮渡人员的选择时间和目的地为其分配座位。就像下面的代码一样,如果用户输入10的时间和输入L的目的地,他们将被分配给1号渡轮,并且列表中的零之一将变成1以指示现在就座。它将继续这样做,直到充满为止。

编辑:我绝对是python的初学者,所以如果我的代码看起来丑陋,请原谅我

economy_class1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
economy_class2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]


name = input("Please enter your name: ")
time = int(input("Enter hour of time of departure: "))
destination = input("Penang or Langkawi? Type P or L: ")
if time == 10 and destination == 'L':
    economy_class1 [0] = 1
    economy_class1 [2] = 1
    economy_class1 [4] = 1
    economy_class1 [5] = 1
    economy_class1 [37] = 1
    economy_class1 [38] = 1
    economy_class1 [39] = 1

    for i, seat in enumerate(economy_class1):
        if seat == 0:
            economy_class1[i] = 1


            print("Here is your ticket")
            print(name +", " "E" + str(i), "Economy Class," "To Langkawi," "From Penang")
            print("Please enjoy your trip!")
            print("********************************")
            cont = input("Type Yes to continue using the program, Type No to quit the program")
            if cont == 'No':
                menu_guard = False
                exit_program()
                back_to_menu = False
                submenu_guard = False
                guard = False
            elif cont == 'Yes':

                guard = True
            break
    else:
        print("All seats are taken")

if time == 11 and destination == 'P':
    economy_class2 [0] = 1
    economy_class2 [1] = 1
    economy_class2 [2] = 1
    economy_class2 [3] = 1
    economy_class2 [4] = 1
    economy_class2 [5] = 1
    economy_class2 [36] = 1
    economy_class2 [37] = 1
    economy_class2 [38] = 1
    economy_class2 [39] = 1

    for i, seat in enumerate(economy_class2):
        if seat == 0:
            economy_class2[i] = 1


            print("Here is your ticket")
            print(name +", " "E" + str(i), "Economy Class," "To Penang," "From Langkawi")
            print("Please enjoy your trip!")
            print("********************************")
            cont = input("Type Yes to continue using the program, Type No to quit the program")
            if cont == 'No':
                menu_guard = False
                exit_program()
                back_to_menu = False
                submenu_guard = False
                guard = False
            elif cont == 'Yes':

                guard = True
            break


    else:
        print("All seats are taken")


else:


    choice = input("No Available Ferry at this hour, type Yes to try again and No to quit the porgram: ")

1 个答案:

答案 0 :(得分:0)

我认为这只是一个正常的错误。 假设您输入名称= xyz,时间= 10,目的地=L。通过分配座位,您可以得到预期的输出。然后,当系统询问“输入是以继续使用该程序时,请键入否以退出该程序”,并说您输入“是”。它使变量“ guard” = True的值从for循环中断。然后在此'if time == 10并且destination =='L''块之后,还有另一个块'if time == 11 and destination =='P':'。有两个连续的if语句。在这里,它会再次检查时间值,并且时间当然不等于11,因此它将移至其他部分。 尝试使第二个与elif相同。

elif time == 11 and destination == 'P':