Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Jonah Monaghan
- # 11/9/17
- # Cows and Bulls
- import random
- import os
- import sys
- numbers = [0, 0, 0, 0]
- eog = False
- # 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: Highscore 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("9999") # 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 = 9999 # If a value error occurs hardcode the highscore
- def GameState(highScore):
- guesses = 0
- while eog == False:
- guesses += 1
- bulls = 0
- cows = 0
- userInput = input("Please enter a 4 digit number: ")
- if len(userInput) != 4:
- print("Error Code 003: Input must be a 4 digit number")
- else:
- uInList = list(userInput)
- # conversion
- for x in range(4):
- uInList[x] = int(uInList[x]) # Uneccessary but rubric says that INTEGERS must be compared
- # comparisson
- for x in range(4):
- if (numbers[x] == uInList[x]):
- bulls += 1
- else:
- # print("Else trigger") [troubleshooting]
- for y in range(4):
- if x != y and numbers[x] == uInList[y]:
- # print("Match found at index ", y, " with number ", numbers[x]) [troubleshooting]
- cows += 1
- print("Bulls: ", bulls)
- print("Cows:", cows)
- if (bulls == 4):
- if guesses < highScore:
- highScoreFile = open("Files\\highscore.txt", "w") # Open highscore file
- highScoreFile.write(str(guesses)) # Rewrite highscore file
- highScoreFile.close() # Close highscore file
- highScore = guesses # Set new highscore
- print("NEW HIGHSCORE") # Print congrats
- print("CONGRATS YOU GOT THE NUMBER IN", guesses, " GUESSES! Would you like to play again? [y/n]")
- userInput = input("") # Not adding additional verification loop but code can be found on High/Low game
- if userInput == "y":
- numGen(highScore)
- elif userInput == "n":
- sys.exit(0)
- def numGen(highScore):
- counter = 0
- while counter < 4:
- fail = False
- tempNumber = random.randint(1, 9)
- for x in range(4):
- if tempNumber == numbers[x]:
- fail = True
- # print("Fail triggered on ", counter, " with number ", tempNumber) [troubleshooting]
- if fail == False:
- numbers[counter] = tempNumber
- counter += 1
- # print(numbers) [troubleshooting]
- GameState(highScore)
- numGen(userHighScore)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement