Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1.  
  2. import random
  3.  
  4.  
  5. def main():
  6.     heading()
  7.     rand_num = number()
  8.     take_guess(rand_num)
  9.  
  10.  
  11. # This function shows the greetings
  12. def heading():
  13.     print("Welcome to the my guessing game!\n")
  14.  
  15.  
  16. # This function generates the random number and displays it
  17. def number():
  18.     number = random.randint(1, 100)
  19.     print("(The answer is ", number, ")", sep="")
  20.  
  21.     return number
  22.  
  23.  
  24. # This function performs the logic required to keep track of the
  25. # guesses in addition to calling other functions that perform
  26. # checks for the boundaries
  27. def take_guess(rand_num):
  28.     start = 1
  29.     end = 100
  30.     counter = 0
  31.     guess = 0
  32.     flag = True
  33.  
  34.     while flag:
  35.         # TODO:
  36.         # When an error is triggered, display only the exception
  37.         # error prompt according to the requirement
  38.         try:
  39.             guess = int(input("Please guess a number between "
  40.                               + str(start) + " and " + str(end) + ": "))
  41.         except ValueError:
  42.             print("Your guess must be a number.\n")
  43.  
  44.         # This function call checks if the user is outside the
  45.         # boundaries and keeps track of guesses
  46.         # if within boundaries
  47.         proceed = boundary_check(guess, start, end, counter)
  48.         if proceed != -1:
  49.             counter += 1
  50.  
  51.         # This function call takes in the start and end
  52.         # boundaries, the guess, the random number, guess
  53.         # counter, and continues if proceed isn't -1
  54.  
  55.         # here you set new_bound but never passed end so that it can be changed
  56.         new_bound, new_end = high_low(start, end, guess, rand_num, proceed, counter)
  57.         start = new_bound
  58.         end = new_end
  59.  
  60.         if start == -1:
  61.             flag = False
  62.  
  63.  
  64. # This function checks the boundaries and returns the proceed
  65. # code in which case -1 means "don't proceed".
  66.  
  67.  
  68. def boundary_check(guess, start, end, counter):
  69.     if guess < start or guess > end:
  70.         print("Your guess must be between " + str(start) +
  71.               " and " + str(end), "\n")
  72.         return -1
  73.  
  74.     else:
  75.         counter += 1
  76.         return counter
  77.  
  78.  
  79. # This function calculates if the guess is high or low and
  80. # resets the boundary based on the calculation
  81.  
  82. # added end so that it can also be changed.
  83. def high_low(start, end, guess, rand_num, proceed, counter):
  84.     if proceed != -1:
  85.         if guess < rand_num:
  86.             print("Too low!\n")
  87.             start = guess
  88.  
  89.         elif guess == rand_num:
  90.             print("You guessed it!")
  91.             print("It only took you", counter, "guess(es)!\n")
  92.             start = -1
  93.             play_again()
  94.  
  95.         elif guess > rand_num:
  96.             end = guess
  97.             print("Too high!\n")
  98.  
  99.     return start, end
  100.  
  101.  
  102. # This function ensures the user types the requested string to
  103. # play again or quit
  104. def play_again():
  105.     flag = True
  106.  
  107.     while flag:
  108.         again = input(str("Play again? (Y/N): "))
  109.         if again == "Y" or again == "y":
  110.             flag = False
  111.             main()
  112.  
  113.         elif again == "N" or again == "n":
  114.             flag = False
  115.  
  116.         else:
  117.             print("The only valid choices are (Y/N)\n")
  118.  
  119.  
  120. # TODO:
  121. # Thank the user for playing
  122. # display the user's best round
  123.  
  124. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement