Advertisement
Guest User

Number Guessing Game Mark Crabtree

a guest
Oct 11th, 2018
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import random
  2. import os
  3. import time
  4.  
  5. '''
  6. Number Guessing Game - Mark Crabtree
  7. This game will allow the user to think of a number and the computer will attempt to guess it or vice versa
  8. Release 1.0 - 10/11/2018
  9. '''
  10. def compThink():
  11.     #set primary variables, and clear the screen
  12.     guessedCorrectly = False
  13.     userInput = 0
  14.     os.system('cls')
  15.     #generate random number for target
  16.     targetNum = random.randint(1, 1000)
  17.     print("Ok I am thinking of a number between 1 and 1000")
  18.    
  19.     #Main guessing Loop, Check users guess and advise them of their status.
  20.     while guessedCorrectly == False:
  21.         print("Please Enter a Guess: ")
  22.         userInput = int((input()))
  23.         if userInput < targetNum:
  24.             print("Nope thats not it. The number is higher.")
  25.         if userInput > targetNum:
  26.             print("Nope thats not it. The number is lower.")  
  27.         if userInput == targetNum:
  28.             print("Congratulations! You guessed it!")
  29.             guessedCorrectly = True
  30.      
  31.         #Wait for user, then activate Main Menu
  32.     input()
  33.     main_menu()
  34.  
  35.        
  36.    
  37. def userThink():
  38.     #set primary variables, and clear the screen
  39.     guessedCorrectly = False
  40.     userInput = ""
  41.     lowerBound = 1
  42.     upperBound = 1000
  43.     #set computers initial guess with a pre set upper and lower bounds
  44.     currentGuess = random.randint(lowerBound, upperBound)
  45.     os.system('cls')
  46.     print("Ok, Please Think of a NUmber between 1 and 1000")
  47.     print("I will attempt to guess the number, you can respond with h for higher, l for lower, or c for correct!")
  48.     print()
  49.     time.sleep(1.5)
  50.    
  51.     #begin guess loop - adjust lower and upper bounds as guess is narrowed down by the user.
  52.     while guessedCorrectly == False:
  53.         userInput = input("Is your number " + str(currentGuess)  + "?")
  54.        
  55.         if userInput == "h":
  56.             lowerBound = currentGuess + 1;
  57.             currentGuess = random.randint(lowerBound, upperBound)
  58.             time.sleep(1)
  59.         elif userInput == "l":
  60.             upperBound = currentGuess - 1;
  61.             currentGuess = random.randint(lowerBound, upperBound)
  62.             time.sleep(1)
  63.         elif userInput == "c":
  64.             print("Congratulations to me! I guessed it!")
  65.             guessedCorrectly = True
  66.         else:
  67.             #Input Validation
  68.             print("Please enter an h for higher, l for lower, or c for correct")
  69.    
  70.     #Wait for user, then activate Main Menu
  71.     input()
  72.     main_menu()
  73.        
  74.        
  75.    
  76.  
  77.  
  78. def main_menu():
  79.     #Main Menu loop
  80.     #clear the console screen
  81.     os.system('cls')
  82.    
  83.     #Present Menu Options and check for input validation
  84.     print("Welcome to the Number Guessing Game 1.0")
  85.     print("---------------------------------------")
  86.     print("Please choose from one of the options below:")
  87.     print()
  88.     print("1. The computer will think of a number")
  89.     print("2. The user will think of a number")
  90.     print("3. Exit the Game")
  91.     print()
  92.    
  93.     #store Users Response and Validate
  94.     userInput = (input())
  95.    
  96.     if userInput == "1":
  97.         compThink()        
  98.     elif userInput == "2":
  99.         userThink()        
  100.     elif userInput == "3":
  101.         exit()
  102.     else:
  103.         main_menu()
  104.        
  105. #Main Menu Loop call
  106. main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement