繁体   English   中英

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行中的每行上,将是两个整数:该轮的Antonia滚动,后跟一个空格,然后是该轮的David滚动。 每卷将是1到6(含)之间的整数。

输出期望:输出将包括两行。 在第一行上,输出在所有回合之后Antonia拥有的积分数。 在第二行中,输出在所有回合之后David拥有的积分数。

输入:

  1. 4

  2. 5 6

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

输出:

  • 100 <-(为什么???)
  • 94

为什么最低值(大卫)应正确更改,但最高值未正确? 我对antonia做了什么不同,从而使其输出的功能与大卫不同?

在第一个循环中,您不断更新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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM