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)
答案 0 :(得分:1)
for循环缩进有一些问题:
这是正确的循环代码,并添加了一个计算点的函数,因此您无需多次编写相同的代码,记住始终定义函数而不是一次又一次地重写相同的代码:
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_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),
})