Reverse game python -
hello know asked couple of times couldnt find answer. question reverse guess number game. code executes program not in "humanlike" way. if number 50 , guesses 20 responds higher, computer says 30 example, gets response lower guesses 15. how solve this? exercise comes from: python absolute beginner. can me? in simple way please because otherwise skip things in book. think can see know , not seeing code. please me...
code:
#guess number # #the computer picks random number between 1 , 100 #the playes tries guess , coputer lets #the player know if guess high, low #or right on money print ("\t// // // // // // // // // //") print ("\twelcome 'guess number'!") print ("\tcomputer vs human") print ("\t// // // // // // // // // //") name = input("what's name?") print ("hello,", name) print ("\nokay, think of number between 1 , 100.") print ("i'll try guess within 10 attemps.") import random #set initial values the_number = int(input("please type in number guess:")) tries = 0 max_tries = 10 guess = random.randint(1, 100) #guessing loop while guess != the_number , tries < max_tries: print("is it", guess,"?") tries += 1 if guess > the_number , tries < max_tries: print ("it's lower") guess = random.randint(1, guess) elif guess < the_number , tries < max_tries: print ("it's higher") guess = random.randint(guess, 100) elif guess == the_number , tries < max_tries: print("woohoo, guessed it!") break else: print("haha silly computer was", the_number,"!") input ("\n\nto exit, press enter key.")
you need keep track of highest possible value , lowest possible value can guess intelligently.
initially, lowest possible value 1 , highest 100. let's guess 50, , computer responds "higher". happens 2 variables? lowest possible value becomes 50, since number cannot lower that. highest value remains same.
if computer responds "lower", opposite happens.
then guess between lowest , highest possible values:
random.randint(lowest, highest)
and guesses work expected.
Comments
Post a Comment