Skip to content

Instantly share code, notes, and snippets.

@liuderchi liuderchi/guess_num.py
Last active Jan 4, 2018

Embed
What would you like to do?
classical game: Guess Number for 4A0B !
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
You can’t perform that action at this time.