Reputation: 15
My issue is when running the below program it simply breaks after max attempts but does NOT print the "I'm out of guesses and you cheated.... Why not?
# 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.
import random
import math
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
maxattempt = math.ceil(math.log(larger-smaller))
count = 0
guess = int((smaller + larger)/2)
while count != maxattempt:
count += 1
guess = int((smaller + larger)/2)
print(smaller,larger)
print("Your number is: ",guess)
hlp = input("Enter =, <, or >: ")
if hlp == '>':
smaller = guess+1
elif hlp == '<':
larger = guess-1
elif hlp == '=':
print("Hooray, I've got it in", count, "tries!")
else:
print("I'm out of guesses, and you cheated")
break
Upvotes: 0
Views: 6262
Reputation: 11
Here is the correct calculation for the "maxattempt" if you read the comments after the original guess the number game, Chapter 3. Lambert's Fundamentals of Python:First Programs with 2021 Updates, 1 term Instant Access - 2nd Edition
Thank you very much Anupam Chaplot. I came here because I wrote my own algorithm for guessing and wasn't getting the correct sequence. Thank you
"""
Program:guess.py
Author:Phillip
Date:4/15/2022
1)In this game the computer tries to guess your number
2)You enter a number range and the computer
tries to guess your number before it runs
out of attempts
3)You answer greater,less,or equal as >,<,or =
to help the computer guess your number
"""
import random#import random library
import math#import math library
#enter your number range
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
maxguess = math.log2(larger-smaller+1)#number of gusses
count = 0 #keeps track of guess count
while count < maxguess:#guessing loop
count += 1
print(smaller,larger)#print current range
guess = int((smaller + larger) / 2)#computer takes a guess
print('number is',guess)
hint = input('Enter >,<,or =:')#you answer greater,less,or equal
if hint == '<':
larger = guess
elif hint == '>':
smaller = guess
elif hint == '=':
print('Hooray, I\'ve got it in %d tries!'%(count))
break
else:
print('I\'m out of guesses, and you cheated')
Upvotes: 0
Reputation: 1316
Use below logic, add if condition outside while loop.
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
maxattempt = math.ceil(math.log(larger - smaller))
count = 0
guess = int((smaller + larger) / 2)
while count != maxattempt:
count += 1
guess = int((smaller + larger) / 2)
print(smaller, larger)
print("Your number is: ", guess)
hlp = input("Enter =, <, or >: ")
if hlp == '>':
smaller = guess + 1
elif hlp == '<':
larger = guess - 1
elif hlp == '=':
print("Hooray, I've got it in", count, "tries!")
break
else:
print("I'm out of guesses, and you cheated")
Upvotes: 1
Reputation: 3152
Because the while loop stops if the attempts are maxed out, so within the loop the else will never be met. Try putting the final string outside the while loop.
Upvotes: 0