initial commit :D

This commit is contained in:
deadvey 2024-11-14 23:52:12 +00:00
parent a54a2ea32b
commit ef2e811b47
2 changed files with 160 additions and 1 deletions

View File

@ -1,3 +1,19 @@
# bases-game
An game for revision base 2, 10 and 16 (for my a level)
An educational game for revision base 2, 10 and 16 (for my a level)<br/>
See chapters 28 - 32 (Section 6) of the PG OCR revision guide for Computer Science AS and A level for the "rules"<br/>
https://www.pgonline.co.uk/resources/computer-science/a-level-ocr/ocr-a-level-textbook/<br/>
# What can you "play" currently?
- Conversions between bases (integers)<br/>
- Binary addition<br/>
- Floating point binary to denery conversion<br/>
# TODO
- Two's complement<br/>
- Sign and magnitude<br/>
- Negative binary<br/>
- Complex binary* <br/>
# Why the fuck is this a thing?
I was fucking revision my fucking as levels and I wanted a program to auto make binary and other bases questions up so I could just rattle through them so yeah.

143
bases-game.py Normal file
View File

@ -0,0 +1,143 @@
print("")
import random
while True:
'''
Conversion ----- 2 -> 10
|- 2 -> 16
|- 10 -> 2
|- 10 -> 16
|- 16 -> 2
|- 16 -> 10
Binary Addition
Floating point binary
Twos compliment, Sign & Magnitude, Negative binary, Complex binary
'''
type_int = random.randint(0,2)
if type_int == 2:
print("What is this binary floating point number in denery?")
int_length = random.randint(0,7)
decimal_length = random.randint(1,7)
answer = float(random.randint(0,2**int_length))
start_int = bin(int(answer))[2:]
start_frac = ""
for x in range(1,decimal_length):
rand = random.randint(0,1)
answer+=(float(rand) * 2**(0-x))
start_frac = start_frac + str(rand)
exponent = bin(len(start_int) - 1)[2:]
print(start_int+start_frac," exp:", exponent)
attempt = input()
if float(attempt) == answer:
print("Correct")
else:
print(f"No, the answer was {answer}")
if type_int == 1:
answer_in_bin_or_den = random.randint(0,1)
if answer_in_bin_or_den == 1:
print("Answer in Base 10")
if answer_in_bin_or_den == 0:
print("Answer in Base 2")
numbers_to_add = random.randint(2,2)
numbers = []
answer = 0
for x in range(numbers_to_add):
current_number = random.randint(0,127)
numbers.append(current_number)
answer = answer + current_number
print(format(current_number, '#010b')[2:],end="")
if (x > 0):
print("+")
else:
print("")
attempt = input()
if answer_in_bin_or_den == 1:
if int(attempt) == answer:
print("Correct")
else:
print(f"No, the answer was {answer}")
if answer_in_bin_or_den == 0:
if str(bin(int(attempt,2)))[2:] == str(bin(answer))[2:]:
print("Correct")
else:
print(f"No, the answer was {bin(answer)}")
if type_int == 0:
initial_base_int = random.randint(0,2)
match initial_base_int:
case 0:
initial_base = 2
case 1:
initial_base = 10
case 2:
initial_base = 16
case _:
print("Error")
final_base_int = random.randint(0,2)
while final_base_int == initial_base_int:
final_base_int = random.randint(0,2)
match final_base_int:
case 0:
final_base = 2
case 1:
final_base = 10
case 2:
final_base = 16
case _:
print("Error")
print(f"Base {initial_base} to Base {final_base}\n")
start_number = random.randint(0,255)
if initial_base == 2:
print(str(bin(start_number))[2:])
if initial_base == 10:
print(start_number)
if initial_base == 16:
print(str(hex(start_number))[2:])
if final_base == 2:
print("")
print("128")
print("|64")
print("||32")
print("|||16")
print("||||8421")
print("||||||||")
print("VVVVVVVV")
if final_base == 10:
print("")
print("100")
print("|10")
print("||1")
print("|||")
print("VVV")
if final_base == 16:
print("")
print("16")
print("|1")
print("||")
print("VV")
attempt = input()
if final_base == 16:
if str(hex(int(attempt,16)))[2:] == str(hex(start_number))[2:]:
print("Correct")
else:
print(f"No, the answer was {hex(int(start_number))[2:]}")
if final_base == 10:
if attempt == str(int(start_number)):
print("Correct")
else:
print(f"No, the answer was {int(start_number)}")
if final_base == 2:
if str(bin(int(attempt,2)))[2:] == str(bin(start_number))[2:]:
print("Correct")
else:
print(f"No, the answer was {bin(start_number)[2:]}")
print("------------------------------------")