Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import os
- import time
- '''
- Number Guessing Game - Mark Crabtree
- This game will allow the user to think of a number and the computer will attempt to guess it or vice versa
- Release 1.0 - 10/11/2018
- '''
- def compThink():
- #set primary variables, and clear the screen
- guessedCorrectly = False
- userInput = 0
- os.system('cls')
- #generate random number for target
- targetNum = random.randint(1, 1000)
- print("Ok I am thinking of a number between 1 and 1000")
- #Main guessing Loop, Check users guess and advise them of their status.
- while guessedCorrectly == False:
- print("Please Enter a Guess: ")
- userInput = int((input()))
- if userInput < targetNum:
- print("Nope thats not it. The number is higher.")
- if userInput > targetNum:
- print("Nope thats not it. The number is lower.")
- if userInput == targetNum:
- print("Congratulations! You guessed it!")
- guessedCorrectly = True
- #Wait for user, then activate Main Menu
- input()
- main_menu()
- def userThink():
- #set primary variables, and clear the screen
- guessedCorrectly = False
- userInput = ""
- lowerBound = 1
- upperBound = 1000
- #set computers initial guess with a pre set upper and lower bounds
- currentGuess = random.randint(lowerBound, upperBound)
- os.system('cls')
- print("Ok, Please Think of a NUmber between 1 and 1000")
- print("I will attempt to guess the number, you can respond with h for higher, l for lower, or c for correct!")
- print()
- time.sleep(1.5)
- #begin guess loop - adjust lower and upper bounds as guess is narrowed down by the user.
- while guessedCorrectly == False:
- userInput = input("Is your number " + str(currentGuess) + "?")
- if userInput == "h":
- lowerBound = currentGuess + 1;
- currentGuess = random.randint(lowerBound, upperBound)
- time.sleep(1)
- elif userInput == "l":
- upperBound = currentGuess - 1;
- currentGuess = random.randint(lowerBound, upperBound)
- time.sleep(1)
- elif userInput == "c":
- print("Congratulations to me! I guessed it!")
- guessedCorrectly = True
- else:
- #Input Validation
- print("Please enter an h for higher, l for lower, or c for correct")
- #Wait for user, then activate Main Menu
- input()
- main_menu()
- def main_menu():
- #Main Menu loop
- #clear the console screen
- os.system('cls')
- #Present Menu Options and check for input validation
- print("Welcome to the Number Guessing Game 1.0")
- print("---------------------------------------")
- print("Please choose from one of the options below:")
- print()
- print("1. The computer will think of a number")
- print("2. The user will think of a number")
- print("3. Exit the Game")
- print()
- #store Users Response and Validate
- userInput = (input())
- if userInput == "1":
- compThink()
- elif userInput == "2":
- userThink()
- elif userInput == "3":
- exit()
- else:
- main_menu()
- #Main Menu Loop call
- main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement