Python骰子游戏点数变量不变

2024-10-01 00:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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行中,每行都是两个整数:那一轮的安东尼娅掷骰子, 接着是空格,接着是那一轮的大卫掷骰子。每次掷骰都是整数 介于1和6之间(包括1和6)。在

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

输入:

  1. 4

  2. 56

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

输出:

  • 100<;--(为什么在
  • 94年

为什么底部值(david)会正确更改,而顶部却没有??我对安东尼娅做了什么不同的事情,使得它不能输出和david相同的函数?


Tags: inforinputifrange整数intdavid
1条回答
网友
1楼 · 发布于 2024-10-01 00:24:35

在第一个循环中,不断更新a和{}。因此,在循环的末尾,a和{}只具有与最后一组输入相对应的值。在

此外,在您的第二个循环中,您不会迭代所有的分数,而是最后一组输入。在进一步讨论之前,我建议您回过头来了解代码的具体操作,并跟踪值是如何变化的。在

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

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)

相关问题 更多 >