Last active
August 29, 2015 14:18
-
-
Save PavelBass/ab9508f7a17e8c050c4f to your computer and use it in GitHub Desktop.
Training. Binary numbers to decimal.
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
# coding: utf-8 | |
import random | |
import string | |
art = 'QlpoOTFBWSZTWX+fdicAABdXgH/9aOegb4oEgAiIBTABGIoaNFTwiZ' \ | |
'opnqjaGpo9qNIMZNMgZNDIMjTAjBIk1HpJPJDIHqaaAwTb/Hi1A36N' \ | |
'sY0QxDaDz7zqW11tpFWwp4mlvVzjatXcNnSgWbbQBPNQkIV85lqm2z' \ | |
'utGc42JYC2SrXjgQoDKzjSQjhGTJX2VemGAgoBWTnFfPwkcu+OEblH' \ | |
'9Xq1ebMV0ygs3CtkvFnhNkwCMoCO+pONKTI7TvvF1xWRdUhpHSURRd' \ | |
'eup4iy2XuLbbr73358vJGeRrwerjahCBn6VTtwSxBAEYMhjAwG0rkA' \ | |
'Vr2vhRVxGWSOjR1tPh2vjO0pFnAwnJswLyC+05Zq48LRgOU/AURkbE' \ | |
'Cv+LuSKcKEg/z7sTg=' | |
from base64 import b64decode | |
from bz2 import decompress | |
print decompress(b64decode(art)) | |
print ''' | |
This program is for training to | |
understand numbers in other systems | |
Enter `quit` or `exit` to quit. | |
Enter `degree {num}` to set num of bits. | |
''' | |
def get_answer(pre='count: '): | |
answer = None | |
while answer is None: | |
try: | |
answer = raw_input(pre) | |
except: | |
print '' | |
answer = None | |
return answer | |
def get_settings(): | |
print "Chose system from (10 by default)" | |
while True: | |
num_from = get_answer() | |
try: | |
num_from = int(num_from) | |
break | |
except: | |
print "What you mean?" | |
print "Chose system to (2 by default)" | |
while True: | |
num_to = get_answer() | |
try: | |
num_to = int(num_to) | |
break | |
except: | |
print "What you mean?" | |
return num_from, num_to | |
def set_degree(a, degree): | |
a = a.split(' ') | |
if a[0].lower()=='degree' and len(a) > 1: | |
try: | |
degree = int(a[1]) | |
except: | |
pass | |
return degree | |
def main(): | |
# num_from, num_to = get_settings() | |
degree = 8 | |
quit_commands = ['quit', 'exit'] | |
while True: | |
num = random.randint(1, 2**(degree)-1) | |
print '>>> %s' % string.zfill(bin(num)[2:], degree) | |
answer = get_answer() | |
if answer.lower() in quit_commands: | |
break | |
_degree = set_degree(answer, degree) | |
if _degree != degree: | |
degree = _degree | |
continue | |
try: | |
answer = int(answer) | |
except: | |
answer = num-1 | |
if answer == num: | |
print 'Great!\n' | |
else: | |
print 'Wrong. It\'s %s' % num | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment