talatham
6/13/2013 - 10:02 AM

## A Mastermind style guessing game. FERMI - right number, right place. PICO - right number, wrong place.

A Mastermind style guessing game.

FERMI - right number, right place. PICO - right number, wrong place.

``````# Mastermind Game (with numbers)

# import Random for creating code
import random

# Create a three digit random number
def secretCode():
secretCode = ''
for i in range(0, 3):
secretCode += str(random.randint(1, 9))
return secretCode

# Let the user guess a number
def userGuess():
while True:
userCode = raw_input('Please guess a three digit number:')
if len(userCode) != 3:  # It must be three digits long
print 'Wrong length of number.'
elif '0' in userCode:
print 'Cannot use zero'
else:
return userCode

def main():
theCode = secretCode()
count = 0

# Continue code until guessed or out of lives
while True:
userCode = userGuess()
result = []
if userCode == theCode:     # If correct guess, end game
print 'MASTERMIND. In ' + str(count+1) + ' turns.'
break
else:
count += 1
if count == 10:     # If the user has had 10 guesses, end game
print 'GAME OVER. It was ' + theCode
break
# For each element of the code...
for i in range(len(theCode)):
# if a number is in the right place, print 'FERMI'
if userCode[i] == theCode[i]:
result += ['FERMI']
# if a number is correct but in the wrong place, print 'PICO'
elif userCode[i] in theCode:
result += ['PICO']
print sorted(result)

if __name__ == '__main__':
main()``````