I very new to coding and trying to figure out how to make code where you input a number and it gives you the fingers needed to display it in binary (if unknown right thumb=1, right index=2 right middle=4 right ring=8 right pinky=16, left pinky=32 left ring=64 left middle=128 left index=256 left thumb=512 you alternate between fingers adding them together to produce numbers)
I tried looking here for input number output letter code here but when I plugged it in it would not function and I don't know enough to fix it.
characters = 'abcdefghij'
d = {}
for x in range(len(characters)):
d[characters[x]] = x+1
print(d)
#prints {'a': 1, 'b': 2, 'c': 4, 'd': 8, 'e': 16, 'f': 32, 'g': 64, 'h': 128, 'i': 256, 'j': 512}
You can do it like this:
def number_to_fingers(number):
fingers = {
'left_thumb': 512,
'left_index': 256,
'left_middle': 128,
'left_ring': 64,
'left_pinky': 32,
'right_pinky': 16,
'right_ring': 8,
'right_middle': 4,
'right_index': 2,
'right_thumb': 1
}
finger_names = []
for finger in fingers:
if number >= fingers[finger]:
finger_names.append(finger)
number -= fingers[finger]
return ", ".join(finger_names)
# Input the number you want to convert to finger names
input_number = int(input("Enter a number: "))
finger_names = number_to_fingers(input_number)
print(f"Output: {finger_names}")
First you map the fingers to their values in the dictionary fingers
. The list finger_names
will store your result. Then you iterate over fingers
, for each finger checking whether the input number is greater than or equal to the value associated with that finger. If it is, the finger name is appended to finger_names
, meaning this finger is needed for the binary representation. The value of the finger is then subtracted from the input number to keep track of the remaining value to be represented. In the end ", ".join(finger_names)
concatenates the finger names into a string so it will look prettier in the printed output.
Here are some nice examples for how to write python code that converts decimal number to binary.