Python 骰子游戏点数变量未更改

Python Dice Game Points Variables Not Changing

rounds = input()

for i in range(int(rounds)):
    score = input(int())[0:3]
    a = score[0]
    d = score[2]


antonia = 100
david = 100

for scores in score:

    if a < d:
        antonia -= int(a)
    if a > d:
        david -= int(d)
    elif a == d:
        pass

print(antonia)
print(david)

输入期望: 输入的第一行包含整数n(1≤n≤15),这是轮次 将播放。在接下来的 n 行中的每一行,将是两个整数:该轮安东尼娅的掷骰, 接着是 space,然后是该轮的 David 掷骰。每个卷将是一个整数 介于 1 和 6(含)之间。

输出预期:输出将由两行组成。在第一行,输出 Antonia 拥有的点数 所有回合结束后。在第二行,输出 David 拥有的点数 所有回合结束后。

输入:

  1. 4

  2. 5 6

  3. 6 6
  4. 4 3
  5. 5 2

输出:

为什么底部值(david)被正确更改了,但顶部却没有??我对 antonia 做了什么不同,这使得它不输出与 david 相同的功能?

在您的第一个循环中,您不断更新 ad。因此,在循环结束时,ad 仅具有与最后一组输入对应的值。

此外,在您的第二个循环中,您没有遍历所有分数,而是遍历最后一组输入。在继续之前,我建议您回过头来了解您的代码到底在做什么,并跟踪值是如何变化的。

无论如何,解决您的问题的一种方法是:

rounds = input("Number of rounds: ")
scores = []
for i in range(int(rounds)):
    score = input("Scores separated by a space: ").split()
    scores.append((int(score[0]), int(score[1]))) #Append pairs of scores to a list

antonia = 100
david = 100

for score in scores:
    a,d = score # Split the pair into a and d
    if a < d:
        antonia -= int(a)
    if a > d:
        david -= int(d)
    elif a == d:
        pass

print(antonia)
print(david)