Agentew04

binary algorithm

Mar 6th, 2021
792
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random as rng
  2. import math
  3.  
  4. def highlow():
  5.     if(guess<correctnum):
  6.         return "high"
  7.     if(guess>correctnum):
  8.         return "low"
  9.     if(guess == correctnum):
  10.         return "right"
  11.  
  12. maxnum=1000
  13. minnum=1
  14. correctnum=rng.randint(minnum,maxnum)
  15. guess=0
  16. guessablenumbers=maxnum
  17. numberOfGuesses=0
  18.  
  19. print("numero certo:", correctnum)
  20. print("maximum guesses:", round(math.log2(maxnum)))
  21.  
  22. while(guess!=correctnum):
  23.     guessablenumbers=maxnum-minnum
  24.     guess=round((guessablenumbers/2)+minnum)
  25.     numberOfGuesses+=1
  26.     result=highlow()
  27.     if(result=="high"):
  28.         minnum=guess
  29.         print(guess, result+"er" )
  30.         continue
  31.     if(result=="low"):
  32.         maxnum=guess
  33.         print(guess, result+"er" )
  34.         continue
  35. print('adivinhado!')
  36. print("number of guesses" + str(numberOfGuesses))
  37. print("guessed number:" +str(correctnum))
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×