简体   繁体   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)

Input Expectation: The first line of input contains the integer n (1 ≤ n ≤ 15), which is the number of rounds that will be played. 输入期望值:输入的第一行包含整数n(1≤n≤15),这是将进行的回合数。 On each of the next n lines, will be two integers: the roll of Antonia for that round, followed by a space, followed by the roll of David for that round. 在接下来的n行中的每行上,将是两个整数:该轮的Antonia滚动,后跟一个空格,然后是该轮的David滚动。 Each roll will be an integer between 1 and 6 (inclusive). 每卷将是1到6(含)之间的整数。

Output Expectation: The output will consist of two lines. 输出期望:输出将包括两行。 On the first line, output the number of points that Antonia has after all rounds have been played. 在第一行上,输出在所有回合之后Antonia拥有的积分数。 On the second line, output the number of points that David has after all rounds have been played. 在第二行中,输出在所有回合之后David拥有的积分数。

Input: 输入:

  1. 4 4

  2. 5 6 5 6

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

Output: 输出:

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

Why is the bottom value(david) changed as it should correctly, but the top is not?? 为什么最低值(大卫)应正确更改,但最高值未正确? What am I doing different for antonia thats making it not output the same function as david? 我对antonia做了什么不同,从而使其输出的功能与大卫不同?

Within your first loop, you continuously update a and d . 在第一个循环中,您不断更新ad So, at the end of the loop, a and d simply have the values corresponding to the last set of input. 因此,在循环结束时, ad仅具有对应于最后一组输入的值。

Additionally, within your second loop, you are not iterating over all the scores, but rather the very last set of input. 此外,在第二个循环中,您不是要遍历所有分数,而是要遍历最后一组输入。 Before going any further, I would suggest you go back and understand what exactly your code is doing and trace how values change. 在继续之前,我建议您返回并了解您的代码在做什么,并跟踪值的变化。

In any case, one way to solve your problem is: 无论如何,解决问题的一种方法是:

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