Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在python实现的算法中会出现逻辑错误_Python_Algorithm_Error Handling - Fatal编程技术网

为什么在python实现的算法中会出现逻辑错误

为什么在python实现的算法中会出现逻辑错误,python,algorithm,error-handling,Python,Algorithm,Error Handling,下面是一个关于任务的基于文本的Python实现算法。任务是, 可以为一名乘客或一组乘客购买车票。购买时,检查所需的上山和下山列车行程的车票数量是否可用。如果车票可用,请计算包括任何团体折扣在内的总价。更新屏幕显示和总计数据 这是对该任务的描述 “一条电动山地铁路每天往返四次。每次旅行,火车都会在9点、11点、13点和15点上山。火车在10点、12点、14点和16点从山顶返回。每列车有6节车厢,80个座位乘客只能购买回程票;所有车票必须在旅行当天购买。上路费用为25美元,下路费用为25美元。10至

下面是一个关于任务的基于文本的Python实现算法。任务是,


可以为一名乘客或一组乘客购买车票。购买时,检查所需的上山和下山列车行程的车票数量是否可用。如果车票可用,请计算包括任何团体折扣在内的总价。更新屏幕显示和总计数据

这是对该任务的描述

“一条电动山地铁路每天往返四次。每次旅行,火车都会在9点、11点、13点和15点上山。火车在10点、12点、14点和16点从山顶返回。每列车有6节车厢,80个座位乘客只能购买回程票;所有车票必须在旅行当天购买。上路费用为25美元,下路费用为25美元。10至80名乘客组成的团队每10名乘客可获得一张免费机票,前提是他们都一起旅行。乘客在购票时必须预订回程列车和出发列车。乘客可以乘坐下山的下一班火车或更晚的火车返回从山顶出发的最后一班火车上有两节额外的车厢。

列车时间显示在大屏幕上,以及每列列车的可用车票数量。每次订票时,显示屏都会更新。当列车满载时,将显示“关闭”字样,而不是可用车票的数量

当15点钟的车票为0时,当我尝试预订16点钟的车票时,由于有多余的车票可用,它要求输入适当的车票数量,而不是允许我预订车票。

您能告诉我为什么会出现这个错误吗?你能修复这个错误吗?提前谢谢你

仅供参考,我是这个领域的初学者,试图学习基础知识

train_up_time = [9,11,13,15] 
train_down_time = [10,12,14,16]
train_up_ticket =[480]*4
train_down_ticket =[480,480,480,640]
train_up_money =[0.00]*4
train_down_money = [0.00]*4

cost = 25.00
index_up = 0
index_down = 0
ticket_cost = 0
ticket_counter = 0

print("The available time and tickets for the trip Up to the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
    print(train_up_time[ i ], "\t\t\t", train_up_ticket[ i ], "\t\t\t", train_up_money[ i ])

print("The available time and tickets for the trip Down from the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
    print(train_down_time[ i ], "\t\t\t", train_down_ticket[ i ], "\t\t\t", train_down_money[ i ])

selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1.  :  " ))
while selling_ticket == 1:

        time_up = int(input("What time would you like to buy for 9 11 13 15? :  "))
        while time_up != 9 and time_up != 11 and time_up != 13 and time_up != 15 :
                print("Error! please select the appropraite time from the available.")
                time_up = int(input("What time would you like buy for 9 11 13 15? :  "))
                
        time_down = int(input("What time would you like to return 10 12 14 16? :  "))
        while time_up > time_down or ( time_down != 10 and time_down != 12 and time_down != 14 and time_down  != 16) :
                print("Error! please select the appropraite time (you must not select the time below the departure)." )
                time_down = int(input("What time would you like to return 10 12 14 16? :  "))
                
        for count in range(0,4):
                if time_up == train_up_time[ count ]:
                        index_up = count
                if time_down == train_down_time[ count]:
                        index_down = count

        for i in range(0, 4):
                if train_up_ticket [ index_up ] ==  "CLOSED":
                        train_up_ticket [ index_up ] = 0
                if train_down_ticket[ index_down ] == "CLOSED" :
                        train_down_ticket[ index_down ] = 0


        num_ticket = int(input("How many tickets would you like to buy? :  "))
        while  num_ticket > train_up_ticket[ index_up ] or num_ticket > train_down_ticket [ index_down ] :
                        print("Error! Please recheck the availability of the trian ticket")
                        num_ticket = int(input("How many tickets would you like to buy? :  "))
                        
        print("Every 10th passenger is FREE!")
        train_up_ticket [ index_up ] = train_up_ticket [ index_up ] -  num_ticket
        train_down_ticket[ index_down ] = train_down_ticket[ index_down ] - num_ticket
        if num_ticket >= 10:
                ticket_cost = cost * (num_ticket - (num_ticket/10))
        else:
                ticket_cost = cost * num_ticket
        print("Your trip cost:  ", "$", ticket_cost)
        print("You need to pay for both ways")
        print("Therefore, your total trip cost including the discount is :   ", "$", ticket_cost*2 )

        train_up_money[ index_up ] = train_up_money[ index_up ] +  ticket_cost
        train_down_money[ index_down ] = train_down_money[ index_down ] + ticket_cost
        if train_up_ticket [ index_up ] ==  0:
                train_up_ticket [ index_up ] = "CLOSED"
        if train_down_ticket[ index_down ] == 0:
                train_down_ticket[ index_down ] = "CLOSED"        
                
        print("The available time and tickets for the trip Up to the mountain ")
        print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
        for i in range(0, 4):
                print(train_up_time[ i ], "\t\t\t", train_up_ticket[ i ], "\t\t\t", train_up_money[ i ])
        print("The available time and tickets for the trip Down from the mountain ")
        print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
        for i in range(0, 4):
                print(train_down_time[ i ], "\t\t\t", train_down_ticket[ i ], "\t\t\t", train_down_money[ i ])

        selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1.  :  " ))

这里有一个问题:

while  num_ticket > train_up_ticket[ index_down ] or num_ticket > train_down_ticket [ index_down ] :

我认为第一次比较应该是
index\u up

我认为代码中没有任何逻辑问题。以下是您程序中的示例日志:

Would you like buy ticket for trip ? Enter 1 else -1: 1
What time would you like to buy for 9 11 13 15?: 15
What time would you like to return 10 12 14 16?: 16
How many tickets would you like to buy?: 480
Every 10th passenger is FREE!
Your trip cost:   $ 10800.0
You need to pay for both ways
Therefore, your total trip cost including the discount is :    $ 21600.0
The available time and tickets for the trip Up to the mountain 
Available time       Available tickets       Total money of the trip / $ 

9                480                 0.0
11               480                 0.0
13               480                 0.0
15               CLOSED                  10800.0
The available time and tickets for the trip Down from the mountain 
Available time       Available tickets       Total money of the trip / $ 

10               480                 0.0
12               480                 0.0
14               480                 0.0
16               160                 10800.0


Would you like buy ticket for trip ? Enter 1 else -1: 1
What time would you like to buy for 9 11 13 15?: 9
What time would you like to return 10 12 14 16?: 16
How many tickets would you like to buy?: 160
Every 10th passenger is FREE!
Your trip cost:   $ 3600.0
You need to pay for both ways
Therefore, your total trip cost including the discount is :    $ 7200.0
The available time and tickets for the trip Up to the mountain 
Available time       Available tickets       Total money of the trip / $ 

9                320                 3600.0
11               480                 0.0
13               480                 0.0
15               CLOSED                  10800.0
The available time and tickets for the trip Down from the mountain 
Available time       Available tickets       Total money of the trip / $ 

10               480                 0.0
12               480                 0.0
14               480                 0.0
16               CLOSED                  14400.0


Would you like buy ticket for trip ? Enter 1 else -1: 1
What time would you like to buy for 9 11 13 15?: 9
What time would you like to return 10 12 14 16?: 14
How many tickets would you like to buy?: 320
Every 10th passenger is FREE!
Your trip cost:   $ 7200.0
You need to pay for both ways
Therefore, your total trip cost including the discount is :    $ 14400.0
The available time and tickets for the trip Up to the mountain 
Available time       Available tickets       Total money of the trip / $ 

9                CLOSED                  10800.0
11               480                 0.0
13               480                 0.0
15               CLOSED                  10800.0
The available time and tickets for the trip Down from the mountain 
Available time       Available tickets       Total money of the trip / $ 

10               480                 0.0
12               480                 0.0
14               160                 7200.0
16               CLOSED                  14400.0


Would you like buy ticket for trip ? Enter 1 else -1: 
我修复了一些行缩进,您可以检查我的代码:

train_up_time = [9,11,13,15] 
train_down_time = [10,12,14,16]
train_up_ticket =[480]*4
train_down_ticket =[480,480,480,640]
train_up_money =[0.00]*4
train_down_money = [0.00]*4

cost = 25.00
index_up = 0
index_down = 0
ticket_cost = 0
ticket_counter = 0
selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1: "))
while selling_ticket == 1:
    time_up = int(input("What time would you like to buy for 9 11 13 15?: "))
    while time_up != 9 and time_up != 11 and time_up != 13 and time_up != 15 :
        print("Error! please select the appropraite time from the available.")
        time_up = int(input("What time would you like buy for 9 11 13 15?: "))
            
    time_down = int(input("What time would you like to return 10 12 14 16?: "))
    while time_up > time_down or ( time_down != 10 and time_down != 12 and time_down != 14 and time_down  != 16) :
        print("Error! please select the appropraite time (you must not select the time below the departure)." )
        time_down = int(input("What time would you like to return 10 12 14 16?: "))
            
    for count in range(0,4):
        if time_up == train_up_time[ count ]:
            index_up = count
        if time_down == train_down_time[ count]:
            index_down = count

    for i in range(0, 4):
        if train_up_ticket [ index_up ] ==  "CLOSED":
            train_up_ticket [ index_up ] = 0

        if train_down_ticket[ index_down ] == "CLOSED" :
            train_down_ticket[ index_down ] = 0

    # print("time_up: {}, time_down: {}".format(time_up, time_down))
    # print("index_up: {}, index_down: {}".format(index_up, index_down))
    # print("train_up_ticket: {}, train_down_ticket: {}".format(train_up_ticket, train_down_ticket))
    
    num_ticket = int(input("How many tickets would you like to buy?: "))
    while  num_ticket > train_up_ticket[ index_up ] or num_ticket > train_down_ticket[ index_down ]:
        print("Error! Please recheck the availability of the trian ticket")
        num_ticket = int(input("How many tickets would you like to buy?: "))
                    
    print("Every 10th passenger is FREE!")
    train_up_ticket[ index_up ] = train_up_ticket[ index_up ] - num_ticket
    train_down_ticket[ index_down ] = train_down_ticket[ index_down ] - num_ticket
    
    # print("train_up_ticket: {}, train_down_ticket: {}".format(train_up_ticket, train_down_ticket))
    
    if num_ticket >= 10:
        ticket_cost = cost * (num_ticket - (num_ticket/10))
    else:
        ticket_cost = cost * num_ticket
        
    print("Your trip cost:  ", "$", ticket_cost)
    print("You need to pay for both ways")
    print("Therefore, your total trip cost including the discount is :   ", "$", ticket_cost*2 )

    train_up_money[ index_up ] = train_up_money[ index_up ] +  ticket_cost
    train_down_money[ index_down ] = train_down_money[ index_down ] + ticket_cost
    
    if train_up_ticket [ index_up ] ==  0:
        train_up_ticket [ index_up ] = "CLOSED"
        
    if train_down_ticket[ index_down ] == 0:
        train_down_ticket[ index_down ] = "CLOSED"        
            
    print("The available time and tickets for the trip Up to the mountain ")
    print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
    for i in range(0, 4):
        print(train_up_time[ i ], "\t\t\t\t", train_up_ticket[ i ], "\t\t\t\t", train_up_money[ i ])
        
    print("The available time and tickets for the trip Down from the mountain ")
    print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
    for i in range(0, 4):
        print(train_down_time[ i ], "\t\t\t\t", train_down_ticket[ i ], "\t\t\t\t", train_down_money[i])

    # print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n")
    selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1: "))

使用断点()调用。这样更快。添加触发此错误的特定输入集谢谢你,mhawke,但我没有得到预期的结果。如果你能帮助我,我将不胜感激。你的代码正在检查是否有足够的车票可供上下行程使用。这似乎符合你的要求。因此,如果没有足够的车票,你就不能继续预订任何车票。在我看来这是对的。您的预期结果是什么?实际上,在主要描述中提到,160名乘客仍然可以在16点钟购买车票,因此,当我尝试预订16点钟的车票时,出现了一个错误,要求输入适当的号码。“在购买时,检查是否有上山下山所需的火车车票数量。”没有提到可以只购买下山车票。请使用这些附加要求更新您的问题。