target="abc"
maxguesses=3
print
print "==================================================="
print "Guess the target sequence of letters"
print "There are %d letters in the sequence" % len(target)
print "and you are allowed a maximum of %d guesses" % maxguesses
print "==================================================="
for x in range(0,maxguesses):
    print
    guess=raw_input("Enter your guess: ")
    # Check for sensible input
    if len(guess) != len(target):
        print
        print "You entered a sequence with the incorrect number of letters"
        break
    # Convert to lowercase
    guess=guess.lower()
    # Did they get it right?
    if guess == target:
        print "You guessed correctly!"
        break
    # First find number in correct positions
    ncorrect=0
    subtarget=""
    subguess=""
    for x in range(0,len(target)):
        if guess[x] == target[x]:
            ncorrect += 1
        else:
            subguess=subguess+guess[x]
            subtarget=subtarget+target[x]
    if ncorrect == 1:
        print "You have 1 correct letter in the right place"
    elif ncorrect > 1:
        print "You have %d correct letters in the right place" % ncorrect
    # Of the remaining letters find the ones that match
    nincorrect=0
    for x in subguess:
        pos = subtarget.find(x)
        if pos >= 0:
            nincorrect += 1
            subtarget=subtarget[:pos]+subtarget[pos+1:]
    if nincorrect == 1:
        print "You have 1 correct letter in the wrong place"
    elif nincorrect > 1:
        print "You have %d correct letters in the wrong place" % nincorrect
    # Maybe they didn't get any letters at all
    if ncorrect == 0 and nincorrect == 0:
        print "You didn't get any correct letters"
else:
    print
    print "Sorry, you have run out of guesses"
    
