Last active
January 4, 2018 09:35
-
-
Save liuderchi/13fcd9f4c10a2dc22855d12bcbdd3b53 to your computer and use it in GitHub Desktop.
classical game: Guess Number for 4A0B !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
def guess_num(guess, ans): | |
resA, resB = 0, 0 | |
for index, c in enumerate(guess): | |
findAnsRes = ans.find(c) | |
if findAnsRes == index: | |
resA += 1 | |
elif findAnsRes >= 0 : | |
resB += 1 | |
return '%dA%dB' % (resA, resB) | |
def assertions(): | |
assert guess_num('4567', '1234') == '0A1B' | |
assert guess_num('4567', '1567') == '3A0B' | |
assert guess_num('4567', '4567') == '4A0B' | |
assert guess_num('1234', '5678') == '0A0B' | |
if __name__ == '__main__': | |
assertions() | |
res = '' | |
answer = getpass.getpass('set up a 4 digit answer!: ') | |
print 'guess a 4 digit number!\n' | |
while res != '4A0B': | |
res = guess_num(str(input('> ')), answer) | |
print res | |
print 'you win!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment