Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, time
- def menu():
- global guessNumber, statDict
- #Pretty straight forward stuff, nothing to be explained here
- var = False
- tries = 0
- print("Welcome to Mastermind!\
- \nThe rules are:\
- \n1. If you're playing on Easy mode, you'll have to guess a 4 digit number, and will be given what digits you guessed.\
- \n2. If you're playing on Hard mode, the number you'll have to guess will have 5 digits, you won't know the places of the digits you guessed.\
- \n3. You have 10 tries to guess the number.")
- userName = input("What is your name? ")
- print("What difficulty do you want to play on?\ Easy/Hard")
- statDict = {
- "Player name:": userName,
- "Number of tries:": tries
- }
- while var == False:
- userDiff = input("Choice: ")
- #Program takes the user's input and turns it into a lower case string that will be compared to other words
- if userDiff.lower() == "easy":
- confirmation = input("You have chosen Easy mode. Are you sure about it? y/n ")
- if confirmation.lower() == "y" or confirmation.lower() == "yes":
- print("Ok.")
- guessNumber = random.randint(1000, 9999)
- easyMode()
- elif confirmation.lower() == "n" or confirmation.lower() == "no":
- print("You'll be redirected to change your difficulty")
- pass
- else:
- print("Sorry. I didn't get that...")
- pass
- elif userDiff.lower() == "hard":
- confirmation = input("You have chosen Hard mode. Are you sure about it? y/n")
- if confirmation.lower() == "y" or confirmation.lower() == "yes":
- print("Ok.")
- guessNumber = random.randint(10000, 99999)
- hardMode()
- #Program goes thru a 1 sec delay after which it calls the function hardMode, same thing goes for lines 20-23
- elif confirmation.lower() == "n" or confirmation.lower() == "no":
- print("You'll be redirected to change your difficulty")
- else:
- print("Sorry. I didn't get that...")
- # this function is used when the user plays on Easy Mode, nothing special so far, nothing to be explained
- def easyMode():
- global guessNumber, guessedDigits, tries, statDict
- guessNumber = str(guessNumber)
- print(guessNumber)
- #movingNumbers is the digits of the guessNumber, put into a list, the items in this list will be compared to the user input
- #if the input is the same as any item in movingNumbers, the input will occupy the same index from movingNumbers into guessedDigits
- movingNumbers = list(guessNumber)
- guessedDigits = ["_"] * len(movingNumbers)
- whileVariable = False
- while whileVariable == False:
- userGuess = input("Your guess is...")
- #what this for loop does is that it goes thru the whole list, the if statements check if the user input is the same as
- #one of the items in the list, if the item appears twice, both items will be get the same places in guessedDigits
- if userGuess in movingNumbers:
- itmIndx = guessNumber.index(userGuess)
- for i, x in enumerate(movingNumbers):
- if x == userGuess:
- guessedDigits[i] = userGuess
- else:
- guessedDigits[itmIndx] = userGuess
- tries += 1
- print("You're correct.")
- print (" ".join(guessedDigits))
- elif userGuess not in guessNumber:
- tries += 1
- print("You're not correct.")
- if "_" not in guessedDigits:
- print("You Won!",\
- "\nThere's your stats", statDict["Player name:"], ":",\
- "\nTries: ", statDict["Number of tries:"])
- exit()
- else:
- pass
- def hardMode():
- global guessNumber, guessedDigits, tries, statDict
- guessNumber = str(guessNumber)
- print(guessNumber)
- movingNumbers = list(guessNumber)
- guessedDigits = ["_"] * len(movingNumbers)
- whileVariable = False
- while whileVariable == False:
- userGuess = input("Your guess is...")
- if userGuess in movingNumbers:
- itmIndx = guessNumber.index(userGuess)
- for i, x in enumerate(movingNumbers):
- if x == userGuess:
- guessedDigits[i] = userGuess
- else:
- guessedDigits[itmIndx] = userGuess
- tries += 1
- print("You're correct.")
- print (" ".join(guessedDigits))
- elif userGuess not in guessNumber:
- tries += 1
- print("You're not correct.")
- if "_" not in guessedDigits:
- print("You Won!",\
- "\nThere's your stats", statDict["Player name:"], ":",\
- "\nTries: ", statDict["Number of tries:"])
- exit()
- else:
- pass
- menu()
Add Comment
Please, Sign In to add comment