Advertisement
makispaiktis

Upper Lower

Jun 13th, 2021 (edited)
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. from random import randrange
  2.  
  3. # Function 1
  4. def makeGuess(random1):
  5.     return "upper" if random1 <= 50 else "lower"
  6.  
  7. # Function 2
  8. def correctGuess(random1, random2):
  9.     return "upper" if random2 > random1 else "lower"
  10.  
  11. # Function 3
  12. def determineWin(guess, correct):
  13.     return True if guess == correct else False
  14.  
  15. # Function 4
  16. def simulate():
  17.     LIMIT = 10
  18.     wins = 0
  19.     result = ""
  20.     # Initialization
  21.     rounds = 1
  22.     result += ("Round " + str(rounds)) + "\n"
  23.     random1 = randrange(1, 101)
  24.     guess = makeGuess(random1)
  25.     random2 = randrange(1, 101)
  26.     while random2 == random1:
  27.         random2 = randrange(1, 101)
  28.     correct = correctGuess(random1, random2)
  29.     didIWin = determineWin(guess, correct)
  30.     if didIWin == True:
  31.         wins += 1
  32.     result += ("r1 = " + str(random1) + " ----> guess = " + str(guess)) + "\n"
  33.     result += ("r2 = " + str(random2) + " ----> Guess was " + str(didIWin)) + "\n"
  34.     # print()
  35.     while didIWin and rounds < LIMIT:
  36.         rounds += 1
  37.         result += ("Round " + str(rounds)) + "\n"
  38.         random1 = random2
  39.         guess = makeGuess(random1)
  40.         random2 = randrange(1, 101)
  41.         while random2 == random1:
  42.             random2 = randrange(1, 101)
  43.         correct = correctGuess(random1, random2)
  44.         didIWin = determineWin(guess, correct)
  45.         if didIWin == True:
  46.             wins += 1
  47.         result += "r1 = " + str(random1) + " ----> guess = " + str(guess) + "\n"
  48.         result += "r2 = " + str(random2) + " ----> Guess was " + str(didIWin) + "\n"
  49.         # print()
  50.  
  51.     # If someone has a 10-streak win, we return True
  52.     if wins == 10:
  53.         print(result)
  54.         return True
  55.     else:
  56.         return False
  57.  
  58. # MAIN FUNCTION
  59. LIMIT = 10**4
  60. WINS = 0
  61. for i in range(1, LIMIT+1):
  62.     print("**********************************")
  63.     print("Simulation " + str(i))
  64.     simulation = simulate()
  65.     if simulation == True:
  66.         WINS += 1
  67.         print("SUCCESS")
  68.     else:
  69.         print("FAILURE")
  70.     print("**********************************")
  71.     print()
  72.  
  73. # Statistics
  74. print()
  75. percentage = WINS / LIMIT
  76. percentage = round(100 * percentage, 3)
  77. print("Wins = " + str(WINS))
  78. print("Rounds = " + str(LIMIT))
  79. print("Win percentage = " + str(percentage) + "%")
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement