【问题标题】:Why am I getting a logical error in a python implemented algorithm为什么我在 python 实现的算法中出现逻辑错误
【发布时间】:2021-02-07 03:39:27
【问题描述】:

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

“车票可以为单人或一组乘客购买。购买时,请检查所需的上下山列车的车票数量。如果有车票,计算总数价格包括任何团体折扣。更新屏幕显示和总计数据。"

这是此任务的描述。

“一条电动山地铁路每天有四趟回程。每趟火车分别在 9、11、13 和 15 点上山。火车在 10、12、14 点从山顶返回,然后16.每列6节车厢,80个座位。旅客只能购买来回车票,所有车票必须在乘车当天购买。上车费$25,上车费$25 10 至 80 人(含 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.  :  " ))

【问题讨论】:

  • 使用 breakpoint() 调用。这样快得多。
  • 添加触发此错误的特定输入集

标签: python algorithm error-handling


【解决方案1】:

这里有问题:

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

我认为第一次比较应该是index_up

【讨论】:

  • 谢谢 mhawke,但我没有得到预期的结果
  • 如果您能帮助我,我将不胜感激
  • 您的代码正在检查是否有足够的票可用于 上下行程。这似乎符合您的要求。因此,如果任一行程都没有足够的门票,那么您将无法继续预订任何门票。在我看来是对的。您的预期结果是什么?
  • 其实,在主要描述中,它提到了160位乘客仍然可以购买16点的票,所以当我尝试预订16点的票时,会产生一个错误,要求输入适当的数字。
  • “购买时,请检查所需的上下山火车旅行的车票数量。”没有提到可以购买仅下程的车票。使用这些附加要求更新您的问题。
【解决方案2】:

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

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: "))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多