29.2k views
2 votes
Modify the guessing-game program so that the user thinks of a number that the computer must guess.

The computer must make no more than the minimum number of guesses, and it must prevent the user from cheating by entering misleading hints.

Use I'm out of guesses, and you cheated and Hooray, I've got it in X tries as your final output.

(Hint: Use the math.log function to compute the minimum number of guesses needed after the lower and upper bounds are entered.)

And here is the code i wrote that works until one point:

# Modify the code below:
import random
import math

smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))

count = 0

print()
while True:
count += 1
myNumber = (smaller + larger) // 2
print('%d %d' % (smaller, larger))
print('Your number is %d' % myNumber)
choice = input('Enter =, <, or >: ')
if choice == '=':
print("Hooray, I've got it in %d tries" % count)
break
elif smaller == larger:
print("I'm out of guesses, and you cheated")
elif choice == '<':
larger = myNumber - 1
else:
smaller = myNumber + 1

This guessing game works up until this question:

Failed: Test 0-50, when 1 is picked then switched when correctly guessed.

This is the input the automater did:

Enter the smaller number: Enter the larger number:
0 50
Your number is 25
Enter =, <, or >: 0 24
Your number is 12
Enter =, <, or >: 0 11
Your number is 5
Enter =, <, or >: 0 4
Your number is 2
Enter =, <, or >: 0 1
Your number is 0
Enter =, <, or >: 1 1
Your number is 1
Enter =, <, or >: I'm out of guesses, and you cheated
1 1
Your number is 1
Enter =, <, or >:
Traceback (most recent call last):
File "guess.py", line 16, in
choice = input('Enter =, <, or >: ')
EOFError: EOF when reading a line

User Ganesh MB
by
5.2k points

2 Answers

4 votes

Final answer:

To optimize the guessing-game program where the computer guesses a user's number, a binary search mechanism should be used. The minimum number of guesses can be calculated using the math.log function, and the code should include checks to prevent user dishonesty. EOFError occurs when input is expected but not provided, which may be an issue with the automated testing setup.

Step-by-step explanation:

The issue you are facing with the guessing-game program involves the need to ensure the computer makes the optimum number of guesses and handling the situation where the user may be giving incorrect hints. Utilizing a binary search approach helps in minimizing the number of guesses the computer makes. To safeguard against user mistakes or intentional misleading, the program adjusts the values of smaller and larger based on the input received.

To compute the minimum number of guesses, we can use the math.log function in the following way:

max_guesses = math.ceil(math.log(larger - smaller + 1, 2))

This calculation is based on the binary search algorithm where the maximum number of guesses needed to find a number in a sorted list is log2(n), with 'n' being the number of elements in the list. If the count exceeds this max_guesses value, the program can deduce that the user is not providing consistent hints and output "I'm out of guesses, and you cheated".

Your EOFError issue appears to be a separate problem that occurs when input is expected, but there is an end of file or no further input. Make sure that the automated testing environment provides the correct input or has not reached an unexpected end of input.

User Shawn Whinnery
by
5.9k points
4 votes

Final answer:

To ensure the guessing-game program works correctly with the computer as the guesser, implementing a binary search algorithm makes for efficient guesses. Using math.log helps determine the maximum number of guesses, preventing the user from cheating. The complete modified code is provided above.

Step-by-step explanation:

To modify the guessing-game program so that the computer guesses the user's number efficiently without falling for any misleading hints, we need to implement a binary search algorithm. This method involves dividing the search interval in half with each guess and using the user's feedback to narrow down the interval.

The minimum number of guesses needed can be calculated using the math.log function, which should be used to make sure the user doesn't cheat by contradicting their previous hints.

Here is the complete modified code implementing the improvements:

import math
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))

max_guesses = math.ceil(math.log(larger - smaller + 1, 2))
count = 0

while count < max_guesses:
count += 1
myNumber = (smaller + larger) // 2
print('Your number is %d' % myNumber)
choice = input('Enter =, <, or >: ')
if choice == '=':
print("Hooray, I've got it in %d tries!" % count)
break
elif choice == '<':
larger = myNumber - 1
elif choice == '>':
smaller = myNumber + 1
else:
print("Invalid input, please enter =, <, or >.")
continue
if smaller > larger:
print("I'm out of guesses, and you cheated")
break
else:
print("I'm out of guesses, and you cheated")

User Harish
by
6.1k points