2024-11-15 00:52:12 +01:00
|
|
|
print("")
|
|
|
|
import random
|
2024-11-15 02:06:56 +01:00
|
|
|
score = 0
|
|
|
|
answered = 0
|
|
|
|
def correct():
|
|
|
|
global score
|
|
|
|
score+=1
|
2024-11-15 12:56:48 +01:00
|
|
|
print("\nCorrect!")
|
2024-11-15 00:52:12 +01:00
|
|
|
while True:
|
|
|
|
'''
|
|
|
|
Conversion ----- 2 -> 10
|
|
|
|
|- 2 -> 16
|
|
|
|
|- 10 -> 2
|
|
|
|
|- 10 -> 16
|
|
|
|
|- 16 -> 2
|
|
|
|
|- 16 -> 10
|
|
|
|
|
|
|
|
Binary Addition
|
|
|
|
Floating point binary
|
2024-11-15 17:23:23 +01:00
|
|
|
Twos compliment,
|
|
|
|
Sign & Magnitude,
|
2024-11-15 00:52:12 +01:00
|
|
|
'''
|
2024-11-15 17:23:23 +01:00
|
|
|
type_int = random.randint(0,4)
|
2024-11-15 00:52:12 +01:00
|
|
|
|
2024-11-15 17:23:23 +01:00
|
|
|
if type_int == 4:
|
|
|
|
start_base = random.randint(0,1)
|
|
|
|
number_int = random.randint(0,128)
|
|
|
|
number_bin = format(number_int, '#010b')[2:]
|
|
|
|
if random.randint(0,1) == 1: # 50/50 chance of being negative
|
|
|
|
number_int = 0 - number_int
|
|
|
|
number_bin = '1' + number_bin[1:]
|
|
|
|
if start_base == 0: # Binary -> Denery
|
|
|
|
print("\nWhat is this Sign and Magnitude base 2 number in base 10?\n")
|
|
|
|
print(number_bin)
|
|
|
|
print("")
|
|
|
|
print("-")
|
|
|
|
print("|100")
|
|
|
|
print("||10")
|
|
|
|
print("|||1")
|
|
|
|
print("||||")
|
|
|
|
print("VVVV")
|
|
|
|
answer = str(int(number_int))
|
|
|
|
elif start_base == 1: # Denery -> Binary
|
|
|
|
print("\nWhat is this base 10 number in Sign and Magnitude base 2?\n")
|
|
|
|
print(number_int)
|
|
|
|
print("")
|
|
|
|
print("-")
|
|
|
|
print("|64")
|
|
|
|
print("||32")
|
|
|
|
print("|||16")
|
|
|
|
print("||||8421")
|
|
|
|
print("||||||||")
|
|
|
|
print("VVVVVVVV")
|
|
|
|
answer = str(int(number_bin))
|
|
|
|
attempt = input()
|
|
|
|
if int(attempt) == int(answer):
|
|
|
|
correct()
|
|
|
|
else:
|
|
|
|
print(f"No, the answer was {answer}")
|
2024-11-15 12:38:15 +01:00
|
|
|
if type_int == 3:
|
|
|
|
start_base = random.randint(0,1)
|
|
|
|
positive_int = random.randint(0,127)
|
|
|
|
negative_int = 0 - positive_int
|
|
|
|
positive_bin = format(positive_int, '#010b')[2:]
|
|
|
|
negative_bin = ""
|
|
|
|
for x in positive_bin:
|
|
|
|
if x == "0":
|
|
|
|
negative_bin += "1"
|
|
|
|
else:
|
|
|
|
negative_bin += "0"
|
|
|
|
|
|
|
|
negative_bin = bin(int(negative_bin,2) + 1)[2:]
|
|
|
|
if (start_base == 0): # Binary -> Denery
|
|
|
|
print("\nWhat does this two's compliment number represent in base 10?\n")
|
|
|
|
print(negative_bin)
|
|
|
|
answer = str(negative_int)
|
|
|
|
elif (start_base == 1): # Denery -> Binary
|
|
|
|
print("\nWhat does this negative number represent in base 2 (two's compliment)\n")
|
|
|
|
print(negative_int)
|
|
|
|
answer = str(negative_bin)
|
|
|
|
|
|
|
|
attempt = input()
|
|
|
|
if attempt == answer:
|
|
|
|
correct()
|
|
|
|
else:
|
|
|
|
print(f"No, the answer was {answer}")
|
2024-11-15 00:52:12 +01:00
|
|
|
if type_int == 2:
|
2024-11-15 01:12:15 +01:00
|
|
|
print("What is this binary floating point number in denery?\n")
|
2024-11-15 00:52:12 +01:00
|
|
|
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:
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
else:
|
|
|
|
print(f"No, the answer was {answer}")
|
|
|
|
if type_int == 1:
|
2024-11-15 12:56:48 +01:00
|
|
|
answer_in_bin_or_den = random.randint(0,0)
|
2024-11-15 00:52:12 +01:00
|
|
|
if answer_in_bin_or_den == 1:
|
2024-11-15 01:12:15 +01:00
|
|
|
print("Answer in Base 10\n")
|
2024-11-15 00:52:12 +01:00
|
|
|
if answer_in_bin_or_den == 0:
|
2024-11-15 01:12:15 +01:00
|
|
|
print("Answer in Base 2\n")
|
2024-11-15 00:52:12 +01:00
|
|
|
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:
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
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:]:
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
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:]:
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
else:
|
|
|
|
print(f"No, the answer was {hex(int(start_number))[2:]}")
|
|
|
|
if final_base == 10:
|
2024-11-15 01:12:15 +01:00
|
|
|
if str(int(attempt)) == str(int(start_number)):
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
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:]:
|
2024-11-15 02:06:56 +01:00
|
|
|
correct()
|
2024-11-15 00:52:12 +01:00
|
|
|
else:
|
|
|
|
print(f"No, the answer was {bin(start_number)[2:]}")
|
2024-11-15 02:06:56 +01:00
|
|
|
answered+=1
|
|
|
|
print(f"\nScore: {score}/{answered}\n------------------------------------\n")
|