10000 Bit test. Read them bits! · EntropyWorks/practice-python@d9e3412 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9e3412

Browse files
committed
Bit test. Read them bits!
1 parent fe1f4cc commit d9e3412

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

experiments/bit_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
4+
def bit_test():
5+
keep_going = True
6+
while keep_going:
7+
rand_num = random.randint(0, 0xFF)
8+
bits = bin(rand_num)
9+
correct = False
10+
attempts = 1
11+
while not correct:
12+
answer = input('What is {} in decimal? '.format(bits.replace('0b', '').rjust(8, '0')))
13+
attempts += 1
14+
if int(answer) == rand_num:
15+
print('*** Correct! ***')
16+
correct = True
17+
else:
18+
if attempts > 3:
19+
print('The answer is: {}'.format(rand_num))
20+
correct = True
21+
else:
22+
print('Try again.')
23+
24+
25+
def main():
26+
bit_test()
27+
28+
if __name__ == '__main__':
29+
main()

0 commit comments

Comments
 (0)
0