Search code examples
python-3.xmathinputuser-inputgrok

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! Write a game that asks the user to guess the number you are thinking of. (For this game, the number will always be 42.)

The user is allowed 10 guesses, and makes a 'Mistake!' if they guess a number that isn't a multiple of 7. A user can make a maximum of one mistake, 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. 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:

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

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.


Solution

  • You are not incrementing the guessCount when a person is making a mistake. 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.