Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Jonah Monaghan
- ##Sept 26th 2017
- ##Assignment 2 (AKA The High/Low Number Guessing Game)
- # import statements
- import os
- import sys
- from random import randint
- # FNF exception catching and resolution
- try:
- highScoreFile = open("Files\\highscore.txt", "r+") # Attempt to open file
- except FileNotFoundError: # FILE NOT FOUND
- print("Error Code 001: File was not found.") # Display error message
- valid = False # Boolean to approve user input
- while (valid == False): # Until User gives valid input
- userInput = input("Would you like this to be resolved? [y/n]\n") # Ask if user would like error resolved
- if (userInput.lower() == "y"): # yes
- valid = True # Input is valid
- try:
- os.mkdir("Files") # Make a directory
- highScoreFile = open("Files\\highscore.txt", "w") # Write the file
- highScoreFile.close()
- highScoreFile = open("Files\\highscore.txt", "r+")
- highScoreFile.write("100") # Write the score inside the file
- except FileExistsError: # Directory Files exists
- highScoreFile = open("Files\\highscore.txt", "w") # Write file into Files directory
- highScoreFile.close()
- highScoreFile = open("Files\\highscore.txt", "r+")
- highScoreFile.write("100") # Write the score inside the file
- elif (userInput.lower() == "n"): # User is an idiot
- valid = True # Set answer to valid
- print("Closing program...") # Print status
- sys.exit(0) # Exit
- else:
- print("Error Code 002a: Invalid Input (Input must be y or n)") # Print error
- sHighScore = highScoreFile.readline()
- highScoreFile.close()
- try:
- userHighScore = int(sHighScore) #try to make the highscore file into a string
- except ValueError:
- userHighScore = 100 #If a value error occurs hardcode the highscore
- def gameLoop(x, userHighScore): # Made into a function for easier replay
- print("The current highscore is: " + str(userHighScore)) # Print highscore
- if (x == "N"): # If first time playing this session
- print("This is your first time playing this session! Let's see what you can do!") # Print welcome message
- else: # If gameLoop was called recursivley
- print("Last game you got the number in " + str(x) + " guesses.")
- guesses = 0 # Reset guesses
- numberToGuess = randint(0, 100) # Get the number to guess
- correct = False # Make sure the user is wrong
- while (correct == False): # While the user has not guessed the right number
- # Try/Catch for invalid input
- try:
- guesses += 1 # Add a guess
- userInput = int(input("Enter a number:\n")) # Take user's guess
- if (userInput > numberToGuess): # If the guess is too high
- print("Too high") # Print hint
- elif (userInput < numberToGuess): # If the guess is too low
- print("Too low") # Print hint
- else: # Else AKA the guess is perfecet
- print("PERFECT!\nIt took you " + str(guesses) + " guesses.") # Print congrats
- if (guesses < userHighScore): # If there are less guesses than the high score then...
- highScoreFile = open("Files\\highscore.txt", "w") # Open highscore file
- highScoreFile.write(str(guesses)) # Rewrite highscore file
- highScoreFile.close() # Close highscore file
- userHighScore = guesses # Set new highscore
- print("NEW HIGHSCORE") # Print congrats
- correct = True # User was correct
- valid = False
- while (valid == False):
- userInput = input("Would you like to play again? [y/n]\n")
- if (userInput.lower() == "y"):
- valid = True
- gameLoop(guesses, userHighScore)
- elif (userInput.lower() == "n"):
- valid = True
- print("Closing program...")
- sys.exit(0)
- else:
- print("Error Code 002a: Invalid Input (Input must be y or n)") # Print error
- except ValueError:
- valid = False
- print("Error Code 002b: Invalid Input (Input must be an integer)")
- gameLoop("N", userHighScore)
Advertisement
Add Comment
Please, Sign In to add comment