Python If语句覆盖。在for循环中追加

Python If语句覆盖。在for循环中追加,python,Python,If语句覆盖了.append函数。我相信这是因为if语句在for循环中,但我不确定。我将如何解决这个问题。在我添加if语句将骑手的位置转换为点之前,代码一直在工作。我尝试了许多不同的方法让if语句/循环工作,但都没有成功 first_place = 5 second_place = 3 third_place = 1 other_placing = 0 runner1 = input("What is the name of the first runner in your team?") r

If语句覆盖了.append函数。我相信这是因为if语句在for循环中,但我不确定。我将如何解决这个问题。在我添加if语句将骑手的位置转换为点之前,代码一直在工作。我尝试了许多不同的方法让if语句/循环工作,但都没有成功

first_place = 5
second_place = 3
third_place = 1
other_placing = 0

runner1 = input("What is the name of the first runner in your team?")
runner2 = input("What is the name of the second runner in your team?")
runner3 = input("What is the name of the third runner in your team?")

runner_stats = []  # This will end up having 3 entries by the end of the loop.
for name in [runner1, runner2, runner3]:
    race1 = int(input(name + ": Enter rider placing for race 1:"))
    race2 = int(input(name + ": Enter rider placing for race 2:"))
    race3 = int(input(name + ": Enter rider placing for race 3:"))
    race4 = int(input(name + ": Enter rider placing for race 4:"))

if race1 >= 4:
 points_race_1  = (other_placing)
elif race1 == 3:
 points_race_1  = (third_place)
elif race1 == 2:
 points_race_1  = (second_place)
elif race1 == 1:
 points_race_1 = (first_place)

if race2 >= 4:
 points_race_2 = (other_placing)
elif race2 == 3:
 points_race_2 = (third_place)
elif race2 == 2:
 points_race_2 = (second_place)
elif race2 == 1:
 points_race_2 = (first_place)


if race3 >= 4:
 points_race_3 = (other_placing)
elif race3 == 3:
 points_race_3 = (third_place)
elif race3 == 2:
 points_race_3 = (second_place)
elif race3 == 1:
 points_race_3 = (first_place)


if race4 >= 4:
 points_race_4 = (other_placing)
elif race4 == 3:
 points_race_4 = (third_place)
elif race4 == 2:
 points_race_4 = (second_place)
elif race4 == 1:
 points_race_4 = (first_place)

runner_stats.append({
        "RacerName": name,
        "Race1Placing": race1,
        "Race1Points": points_race_1,
        "Race2Placing": race2,
        "Race2Points": points_race_2,
        "Race3Placing": race3,
        "Race3Points": points_race_3,
        "Race4Placing": race4,
        "Race4Points": points_race_4,
    })

print(runner_stats)
if语句之前的工作代码

runner1 = input("What is the name of the first runner in your team?")
runner2 = input("What is the name of the second runner in your team?")
runner3 = input("What is the name of the third runner in your team?")

runner_stats = []  # This will end up having 3 entries by the end of the loop.
for name in [runner1, runner2, runner3]:
    race1 = int(input(name + ": Enter rider placing for race 1:"))
    race2 = int(input(name + ": Enter rider placing for race 2:"))
    race3 = int(input(name + ": Enter rider placing for race 3:"))
    race4 = int(input(name + ": Enter rider placing for race 4:"))

    runner_stats.append({
        "RacerName": name,
        "Race1Placing": race1,
        "Race2Placing": race2,
        "Race3Placing": race3,
        "Race4Placing": race4,
    })

print(runner_stats)

for循环的缩进有一些问题:

  • 编写时的if语句不在for循环内,实际上它在循环外,因为它在同一级别缩进
  • append指令在循环之外,只有最后一个参赛者将被添加到列表中,因此您的问题可能在这里
这是正确的循环代码,添加了一个计算点的函数,因此您不需要多次编写相同的代码,请记住始终定义函数或反复重写相同的代码:

first_place = 5
second_place = 3
third_place = 1
other_placing = 0

runner1 = input("What is the name of the first runner in your team?")
runner2 = input("What is the name of the second runner in your team?")
runner3 = input("What is the name of the third runner in your team?")

runner_stats = []  # This will end up having 3 entries by the end of the loop.

def race_points(race):

    if race >= 4:
     points = (other_placing)
    elif race == 3:
     points = (third_place)
    elif race == 2:
     points = (second_place)
    elif race == 1:
     points = (first_place)

    return points

for name in [runner1, runner2, runner3]:
    race1 = int(input(name + ": Enter rider placing for race 1:"))
    race2 = int(input(name + ": Enter rider placing for race 2:"))
    race3 = int(input(name + ": Enter rider placing for race 3:"))
    race4 = int(input(name + ": Enter rider placing for race 4:"))

    points_race_1 = race_points(race1)
    points_race_2 = race_points(race2)
    points_race_3 = race_points(race3)
    points_race_4 = race_points(race4)    

    runner_stats.append({
            "RacerName": name,
            "Race1Placing": race1,
            "Race1Points": points_race_1,
            "Race2Placing": race2,
            "Race2Points": points_race_2,
            "Race3Placing": race3,
            "Race3Points": points_race_3,
            "Race4Placing": race4,
            "Race4Points": points_race_4,
        })

print(runner_stats)
您也可以在追加时直接使用points函数,而无需定义points\u race变量:

runner_stats.append({
        "RacerName": name,
        "Race1Placing": race1,
        "Race1Points": race_points(race1),
        "Race2Placing": race2,
        "Race2Points": race_points(race2),
        "Race3Placing": race3,
        "Race3Points": race_points(race3),
        "Race4Placing": race4,
        "Race4Points": race_points(race4),
    })

我认为你的意图是错误的。像在工作代码中一样,将所有的if都放入for循环以及runn_stats.append()。@Sharku谢谢。忘记在我上次尝试后重新缩进代码。