我如何解决 Python 中读取过多输入的问题

How I can solve the read too much input problem in Python

这是我的问题:

您在传统 'What number am I thinking of?' 游戏的基础上设计了一个新的变体来帮助您的堂兄弟学习他们的 7 乘法表!编写一个游戏,要求用户猜测您正在考虑的数字。 (对于这个游戏,数字永远是 42。)

用户最多可以猜 10 次,如果猜中的数字不是 7 的倍数,则投 'Mistake!'。用户最多只能错一次,否则输掉游戏。当游戏结束时,你应该总是打印出来 That was fun.

这是一个例子:

Guess a multiple of 7: 14
Nope!
Guess a multiple of 7: 32
Mistake! That number isn't a multiple of 7.
Guess a multiple of 7: 28
Nope!
Guess a multiple of 7: 86
Another mistake. Too bad.
That was fun.

这是另一个例子:

Guess a multiple of 7: 7
Nope!
Guess a multiple of 7: 14
Nope!
Guess a multiple of 7: 126
Nope!
Guess a multiple of 7: 133
Nope!
Guess a multiple of 7: 70
Nope!
Guess a multiple of 7: 77
Nope!
Guess a multiple of 7: 63
Nope!
Guess a multiple of 7: 35
Nope!
Guess a multiple of 7: 126
Nope!
Guess a multiple of 7: 77
Nope!
No guesses left!
That was fun.

如果用户正确输入 42,您的程序应该打印出您赢了!而不是 Nope!,然后不再要求任何猜测。例如:

Guess a multiple of 7: 42
You won!
That was fun.

现在是我的代码:

guessCount = 0
mistakeMade = False

while True:
  guess = int(input("Guess a multiple of 7: "))
  if guess % 7 != 0:
    if mistakeMade:
      print("Another mistake. Too bad.")
      break
    else:
      print("Mistake! That number isn't a multiple of 7.")
      mistakeMade = True
  else:
    if guess == 42:
      print("You won!")
      break
    else:
      print("Nope!")
      guessCount += 1
      if guessCount == 10:
        print("No guesses left!")
        break
        
print("That was fun.")

我通过了 Grok Learning 的所有检查,除了这个错误:

Testing a hidden case. Your submission attempted to read too much input! This occurred on line 5 of your submission.

想了很久还是没有解决问题。希望得到大家的帮助。 谢谢。

你应该每次都进行递增并检查 guessCount,因此将其从 if 条件中删除。


[编辑]旧的无关答案。

也许:

guess = int(input("Guess a multiple of 7: ").split()[0])

?

当有人犯错时,您不会增加 guessCount。试试这个

wrongGuess = 0
for _ in range(10):
    guess = int(input("Guess a multiple of 7: "))
    if guess % 7 != 0:
        wrongGuess += 1
        if wrongGuess > 1:
            print("Another mistake. Too bad.")
            break
        else:
            print("Mistake! That number isn't a multiple of 7.")
            wrongGuess += 1
    else:
        if guess == 42:
          print("You won!")
          break
        else:
          print("Nope!")
else:
    print("No guesses left!")
        
print("That was fun.")

注意:对于像这样的有限计数问题,总是尝试使用 for 循环