breaking-beaker

High/Low Number Game Python

Nov 12th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. ##Jonah Monaghan
  2. ##Sept 26th 2017
  3. ##Assignment 2 (AKA The High/Low Number Guessing Game)
  4.  
  5. # import statements
  6. import os
  7. import sys
  8. from random import randint
  9.  
  10. # FNF exception catching and resolution
  11. try:
  12.     highScoreFile = open("Files\\highscore.txt", "r+")  # Attempt to open file
  13. except FileNotFoundError:  # FILE NOT FOUND
  14.     print("Error Code 001: File was not found.")  # Display error message
  15.     valid = False  # Boolean to approve user input
  16.     while (valid == False):  # Until User gives valid input
  17.         userInput = input("Would you like this to be resolved? [y/n]\n")  # Ask if user would like error resolved
  18.         if (userInput.lower() == "y"):  # yes
  19.             valid = True  # Input is valid
  20.             try:
  21.                 os.mkdir("Files")  # Make a directory
  22.                 highScoreFile = open("Files\\highscore.txt", "w")  # Write the file
  23.                 highScoreFile.close()
  24.                 highScoreFile = open("Files\\highscore.txt", "r+")
  25.                 highScoreFile.write("100")  # Write the score inside the file
  26.             except FileExistsError:  # Directory Files exists
  27.                 highScoreFile = open("Files\\highscore.txt", "w")  # Write file into Files directory
  28.                 highScoreFile.close()
  29.                 highScoreFile = open("Files\\highscore.txt", "r+")
  30.                 highScoreFile.write("100")  # Write the score inside the file
  31.         elif (userInput.lower() == "n"):  # User is an idiot
  32.             valid = True  # Set answer to valid
  33.             print("Closing program...")  # Print status
  34.             sys.exit(0)  # Exit
  35.         else:
  36.             print("Error Code 002a: Invalid Input (Input must be y or n)")  # Print error
  37.  
  38. sHighScore = highScoreFile.readline()
  39. highScoreFile.close()
  40.  
  41.  
  42. try:
  43.     userHighScore = int(sHighScore) #try to make the highscore file into a string
  44. except ValueError:
  45.     userHighScore = 100 #If a value error occurs hardcode the highscore
  46.  
  47. def gameLoop(x, userHighScore):  # Made into a function for easier replay
  48.  
  49.     print("The current highscore is: " + str(userHighScore))  # Print highscore
  50.  
  51.     if (x == "N"):  # If first time playing this session
  52.         print("This is your first time playing this session! Let's see what you can do!")  # Print welcome message
  53.     else:  # If gameLoop was called recursivley
  54.         print("Last game you got the number in " + str(x) + " guesses.")
  55.     guesses = 0  # Reset guesses
  56.     numberToGuess = randint(0, 100)  # Get the number to guess
  57.     correct = False  # Make sure the user is wrong
  58.  
  59.     while (correct == False):  # While the user has not guessed the right number
  60.  
  61.         # Try/Catch for invalid input
  62.         try:
  63.             guesses += 1  # Add a guess
  64.             userInput = int(input("Enter a number:\n"))  # Take user's guess
  65.             if (userInput > numberToGuess):  # If the guess is too high
  66.                 print("Too high")  # Print hint
  67.             elif (userInput < numberToGuess):  # If the guess is too low
  68.                 print("Too low")  # Print hint
  69.             else:  # Else AKA the guess is perfecet
  70.                 print("PERFECT!\nIt took you " + str(guesses) + " guesses.")  # Print congrats
  71.                 if (guesses < userHighScore):  # If there are less guesses than the high score then...
  72.                     highScoreFile = open("Files\\highscore.txt", "w") # Open highscore file
  73.                     highScoreFile.write(str(guesses))  # Rewrite highscore file
  74.                     highScoreFile.close() # Close highscore file
  75.                     userHighScore = guesses # Set new highscore
  76.                     print("NEW HIGHSCORE")  # Print congrats
  77.                 correct = True  # User was correct
  78.                 valid = False
  79.                 while (valid == False):
  80.                     userInput = input("Would you like to play again? [y/n]\n")
  81.                     if (userInput.lower() == "y"):
  82.                         valid = True
  83.                         gameLoop(guesses, userHighScore)
  84.                     elif (userInput.lower() == "n"):
  85.                         valid = True
  86.                         print("Closing program...")
  87.                         sys.exit(0)
  88.                     else:
  89.                         print("Error Code 002a: Invalid Input (Input must be y or n)")  # Print error
  90.  
  91.         except ValueError:
  92.             valid = False
  93.             print("Error Code 002b: Invalid Input (Input must be an integer)")
  94.  
  95.  
  96. gameLoop("N", userHighScore)
Advertisement
Add Comment
Please, Sign In to add comment