简体   繁体   English

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

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

Here is my question:这是我的问题:

You've devised a new twist on the traditional 'What number am I thinking of?'你对传统的“我在想什么数字?”进行了新的改动。 game to help your cousins learn their 7 times tables.帮助您的堂兄弟学习他们的 7 乘法表的游戏。 Write a game that asks the user to guess the number you are thinking of, (For this game. the number will always be 42.)编写一个游戏,让用户猜出你正在想的数字(对于这个游戏,数字永远是 42。)

The user is allowed 10 guesses, and makes a 'Mistake.'允许用户进行 10 次猜测,并出现“错误”。 if they guess a number that isn't a multiple of 7, A user can make a maximum of one mistake.如果他们猜出的数字不是 7 的倍数,则用户最多只能犯一个错误。 otherwise they lose the game, When the game is over.否则他们输了比赛,当比赛结束时。 you should always print out That was fun.你应该总是打印出来那很有趣。

Here is an example:这是一个例子:

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.

Here is another example:这是另一个例子:

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.

If the user correctly enters 42, your program should print out You won, instead of Nope.: and then not ask for any more guesses.如果用户正确输入 42,您的程序应该打印出 You won,而不是 Nope.:,然后不再要求进行任何猜测。 For example:例如:

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

And now is my code:现在是我的代码:

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.")

I passed every check of Grok Learning except for this error:我通过了 Grok Learning 的所有检查,除了这个错误:

Testing a hidden case.测试一个隐藏的案例。 Your submission attempted to read too much input.您提交的内容试图读取过多的输入。 This occurred on line 5 of your submission.这发生在您提交的第 5 行。

I spent a lot of time thinking about it but it still hasn't solved the problem.我花了很多时间思考它,但它仍然没有解决问题。 Hope to receive help from everyone.希望得到大家的帮助。 Thank you.谢谢你。

You are not incrementing the guessCount when a person is making a mistake.当一个人犯错时,您不会增加guessCount Try this out试试这个

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.")

Note: For problems like this with finite count always try using for loop .注意:对于像这样的有限计数问题,总是尝试使用for loop

You should do the incrementation and check for guessCount evertime, so remove it from the if condition.您应该每次都进行递增并检查guessCount ,因此将其从if条件中删除。


[Edit] Old unrelated answer. [编辑] 旧的无关答案。

Maybe:可能是:

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

?

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

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