uccjshrimpton

MasterMind

Mar 13th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. def main():
  2.     from random import randint
  3.  
  4.     def mastermind():
  5.         print("""
  6.                  _                  _           _
  7.  /\/\  __ _ ___| |_ ___ _ __ /\/\ (_)_ __   __| |
  8. /    \ / _` / __| __/ _ \ '__/    \| | '_ \ / _` |
  9. / /\/\ \ (_| \__ \ ||  __/ | / /\/\ \ | | | | (_| |
  10. \/    \/\__,_|___/\__\___|_| \/    \/_|_| |_|\__,_|
  11.        """)
  12.  
  13.     def getnum():
  14.         numbers = [str(randint(1,9)),str(randint(1,9)),str(randint(1,9)),str(randint(1,9))]
  15.         return numbers
  16.  
  17.     def getguess():
  18.         listguess = []
  19.         validguess = False
  20.  
  21.         while validguess == False:
  22.             guess = input("Please guess which four numbers have been selected.\n")
  23.             if guess.isnumeric() == False:
  24.                 print("You may only enter four numbers. For example '1234'.\n")
  25.             elif len(guess) != 4:
  26.                 print("Your guess may only contain four numbers, no more, no less.\n")
  27.             else:
  28.                 guess = list(guess)
  29.                 validguess = True
  30.         return guess
  31.  
  32.     def gameplay():
  33.         mastermind()
  34.         numbers = getnum()
  35.         guess = []
  36.         correctcount = 0
  37.         totalguesses = 0
  38.  
  39.         print("The numbers have been picked.")
  40.         while guess != numbers:
  41.             guess = getguess()
  42.             for randnum, guessnum in zip(numbers,guess):
  43.                 if randnum == guessnum:
  44.                     correctcount += 1
  45.             print("Your guess contained "+str(correctcount)+" correct numbers.\n")
  46.             correctcount = 0
  47.             totalguesses += 1
  48.  
  49.         print("You guessed correctly, well done.")
  50.         print("The answer you were looking for was","".join(numbers)+".")
  51.         print("It took you a total number of",totalguesses,"guesses.")
  52.  
  53.     gameplay()
  54. main()
Advertisement
Add Comment
Please, Sign In to add comment