You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python赛事计时程序逻辑异常:预期执行if分支却进入else分支

赛事胜负计算Python程序分支执行异常问题

我编写了一款用于通过罚时、团队总时长及选手完赛时间判定赛事胜负的Python程序,但运行时出现问题:输入符合预期条件的参数后,程序未执行预期的if分支,反而输出else分支的提示语“Something went wrong. Please try again.”

程序代码

# This is a Python program intended to calculate which team won,
# using penalties, combined times, and run finish times.


# Ask how many snake laps will happened during the tournament
laps = int(input("How many laps happened? (including the last lap, where the players didn't ride the snake): "))

# Ask for the first team's name
t1name = input("What is the first team's name?: ")
# Ask for the second team's name
t2name = input("What is the second team's name?: ")

# Ask for the first player's name in the first team
t1p1 = input("What is " + t1name + "'s first player named?: ")
# Ask for the second player's name in the first team
t1p2 = input("What is " + t1name + "'s second player named?: ")

# Ask for the first player's name in the second team
t2p1 = input("What is " + t2name + "'s first player named?: ")
# Ask for the second player's name in the second team
t2p2 = input("What is " + t2name + "'s second player named?: ")

# Ask in seconds, how fast the first player of the first team finished in
t1p1finish = int(input("In how many seconds did the first player of the first team finish in?: "))
# Ask in seconds, how fast the second player of the first team finished in
t1p2finish = int(input("In how many seconds did the second player of the first team finish in?: "))

# Ask in seconds, how fast the first player of the second team finished in
t2p1finish = int(input("In how many seconds did the first player of the second team finish in?: "))
# Ask in seconds, how fast the second player of the second team finished in
t2p2finish = int(input("In how many seconds did the second player of the second team finish in?: "))

# Declare what t1finish is
t1finish = int(t1p1finish + t1p2finish)
# Declare what t2finish is
t2finish = int(t2p1finish + t2p2finish)

# Ask what the first team's combined time is
t1time = int(input("What is " + t1name + "'s combined time? (in seconds only): "))
# Ask what the second team's combined time is
t2time = int(input("What is " + t2name + "'s combined time? (in seconds only): "))


# Penalty system
# Penalty if the first team is under 180 seconds, the second team is also
# under 180 seconds, and the second team's combined time is higher than the
# first team's combined time
if t1time > 180 and t2time < t1time and t2time > 180:
    t1pftime = ((t2finish - t1finish) * laps + t1time)
    print(t1pftime)
# Penalty if the first team is under 180 seconds, the second team is also
# under 180 seconds, and the first team's combined time is higher than the
# second team's combined time
elif t1time > 180 and t2time > t1time and t2time > 180:
    t2pftime = ((t1finish - t2finish) * laps + t2time)
    print(t2pftime)

else:
    print("Something went wrong. Please try again.")

终端输入输出

$ python goitournament.py 
How many laps happened? (including the last lap, where the players didn't ride the snake): 5
What is the first team's name?: Yeeting
What is the second team's name?: Blasting
What is Yeeting's first player named?: yeet
What is Yeeting's second player named?: adomen
What is Blasting's first player named?: blasbolt
What is Blasting's second player named?: acombo
In how many seconds did the first player of the first team finish in?: 500
In how many seconds did the second player of the first team finish in?: 500
In how many seconds did the first player of the second team finish in?: 500
In how many seconds did the second player of the second team finish in?: 500
What is Yeeting's combined time? (in seconds only): 178
What is Blasting's combined time? (in seconds only): 179
Something went wrong. Please try again.

我原本期望程序执行if分支并打印t1pftime,但实际结果不符合预期。


问题原因

代码中的条件判断和注释描述完全矛盾,导致输入的参数无法匹配预期分支:

  1. 注释说明触发分支的条件是两队总时长低于180秒,但代码中写的是> 180(高于180秒),逻辑完全相反;
  2. if分支的条件中t2time < t1time和注释里的“第二队总时长高于第一队”矛盾,实际应该是t2time > t1time。

修正方案

将罚时判断的条件修改为符合注释描述的逻辑:

# Penalty system
# Penalty if the first team is under 180 seconds, the second team is also
# under 180 seconds, and the second team's combined time is higher than the
# first team's combined time
if t1time < 180 and t2time < 180 and t2time > t1time:
    t1pftime = ((t2finish - t1finish) * laps + t1time)
    print(t1pftime)
# Penalty if the first team is under 180 seconds, the second team is also
# under 180 seconds, and the first team's combined time is higher than the
# second team's combined time
elif t1time < 180 and t2time < 180 and t2time < t1time:
    t2pftime = ((t1finish - t2finish) * laps + t2time)
    print(t2pftime)
else:
    print("Something went wrong. Please try again.")

修正后,输入示例中的参数会匹配if分支,计算并输出178(因为t2finish - t1finish为0,乘以5后仍为0,加上t1time=178)。

内容的提问来源于stack exchange,提问作者Aggressive Combo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 23:47:26