简体   繁体   English

为什么我的程序不计算最后一场比赛的分数?

[英]Why doesn't my program count the the last game scores?

I want to input scores of 30 matches of football game and calculate the number of the winning match and total scores.我想输入30场足球比赛的比分并计算获胜比赛的数量和总比分。 I have 30 input but it doesn't calculate the last match (30th match).我有 30 个输入,但它不计算最后一场比赛(第 30 场比赛)。 What am I to do?我是什么做的?

scores=0
win_number=0
game_number=0
x=int(input())
count=0
while count!=30 :
    if x==3:
        scores=scores+3
        win_number=win_number+1
        game_number=game_number+1
        count=count+1
        x=int(input())
    elif x==1:
        scores=scores+1
        win_number=win_number+1
        x=int(input())
        count=count+1
    elif x==0:
        game_number=game_number+1
        count=count+1
        x=int(input())
else :
    print(scores,'',win_number,game_number)

I will make a simplified use case to explain why your code doesnt work as you think.我将做一个简化的用例来解释为什么你的代码不像你想象的那样工作。 Imagine we only want 1 score.想象一下,我们只想要 1 个分数。 you set count to 0 and read the first input before your loop.您将 count 设置为 0 并在循环之前读取第一个输入。 So you have taken the input before the loop started.所以你在循环开始之前就已经接受了输入。 At this point the count is still set as 0. So you start your loop count != 1 .此时计数仍设置为 0。因此您开始循环count != 1 This takes the input you collected outside the loop and adds to the stats.这需要您在循环外收集的输入并添加到统计信息中。 It then increments the count by 1 so the count now equals 1. You then ask for the input again.然后将计数增加 1,因此计数现在等于 1。然后您再次要求输入。 This second input is given (even though you only wanted 1 match).给出了第二个输入(即使您只想要 1 个匹配项)。 this input is stored in x and the first iteration of the loop ends.此输入存储在 x 中,循环的第一次迭代结束。 the loop condition count!=1 is now broken so the loop finishes after 1 iteration.循环条件count!=1现在已中断,因此循环在 1 次迭代后完成。 so the second input which is allocated to x is never added to the stats.所以分配给 x 的第二个输入永远不会添加到统计信息中。 Which is the correct behaviour.这是正确的行为。 The issue is your code structure meant that you would ask for 1 more input but never count the last one and still get 30 matches问题是您的代码结构意味着您会要求再输入 1 个输入,但从不计算最后一个输入并且仍然获得 30 个匹配项

Instead you can use a range to generate X number of iterations.相反,您可以使用范围来生成 X 次迭代。 Your code can also be cleaned up as there are a lot of lines that occur in each if statement these can be removed from the if and just written once in the loop.您的代码也可以清理,因为每个 if 语句中都有很多行,这些行可以从 if 中删除,只需在循环中写入一次。

scores=0
win_number = 0
game_number = 0
for _ in range(3):
    x = int(input('score: '))
    scores += x
    if x == 3 or x == 1:
        win_number=win_number+1
    if x == 3 or x == 0:
        game_number += 1

print(scores,win_number,game_number)

CONSOLE安慰

score: 3
score: 1
score: 0
4 2 2

Had a little fun refactoring your code.重构你的代码有点乐趣。 This is what I came up with:这就是我想出的:

count = 0
scores = 0
win_number = 0
game_number = 0

while count < 30:
    x = int(input())
    if x not in [0, 1, 3]:
        print("Wrong input - enter either 0, 1 or 3")
        continue
    scores += x
    count += 1
    game_number += 1

    if x == 3 or x == 1: # is x == 1 really a win, though?
        win_number = win_number+1

    print(scores,'',win_number,game_number)

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

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