I am trying to get character matching when comparing two strings. The result would display the two words side by side with the matching (and non-matching) characters displayed. This would be similar to mastermind. Example:
(test word) - HALE (sample word) - HELP
This would be displayed as:
H?L? HELP
and with each subsequent test word, it would update for matches:
H?L? HELP
HEL? HELP
HELP HELP
The code would register each test and display success or failure at the end of the line. I can do that part and even compare the two words. I just need to get the display part working. Here is my code so far:
import string, random, datetime
now = datetime.datetime.now()
tries = 0
result = ""
numchars = 0
def id_generator(size = numchars, chars=string.ascii_uppercase): # size = 6
return ''.join(random.choice(chars)
for _ in range(size))
numchars = int(raw_input("Enter the number of characters in the word : "))
testword = raw_input("What is the word you are testing for? ")
word = id_generator()
while word != testword:
print tries, "tries and word =", word, " test word is ", testword
tries = tries + 1
word = id_generator(numchars)
tries = tries + 1
print tries, "tries and word =", word, " test word is ", testword
now1 = datetime.datetime.now()
print "We have a match! And, it only took ", tries, " attempts!"
print "Total run time:", str(now1 - now)
The current results look like this:
877663 tries and word = GLIS test word is HELP
877664 tries and word = DBAJ test word is HELP
877665 tries and word = NAIF test word is HELP
877666 tries and word = NHVI test word is HELP
877667 tries and word = ZDIZ test word is HELP
877668 tries and word = THCL test word is HELP
877669 tries and word = LMQE test word is HELP
877670 tries and word = OIMU test word is HELP
877671 tries and word = HXGB test word is HELP
877672 tries and word = OGIY test word is HELP
877673 tries and word = BSTG test word is HELP
877674 tries and word = UELJ test word is HELP
877676 tries and word = HELP test word is HELP
We have a match! And, it only took 877676 attempts!
Total run time: 0:07:44.332000
I don't mind the overall output, though I might want to pipe it to a text file later. I don't even mind seeing the test word in full, but character matching is my goal.