Game: Nine Heads and Tails [closed]

Description of Game: Nine coins are placed in a 3×3 matrix with some face up and some face down. You can represent the state of the coins with the values 0(heads) and 1(tails). ##(Here are some examples: 000 101 110 101 010 001 100 110 000 100 001 100)## Each state can also be represented using a binary number. (For example, the preceding matrices correspond to the numbers: 000010000 101001100 110100001 101110100). There are a total of 512 possibilities. So, you can use the decimal numbers 0, 1, 2, 3, …, and 511 to represent all states of the matrix.

My goal is to write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding 3×3 matrix with the characters H and T. ##(ex. “Enter a # between 0 and 511: ” (7) the user entered 7, which corresponds to 000000111. Since 0 stand for H and 1 for T, the output is: HHH HHH TTT) ##

I’m finding this a bit complicated, so I’d appreciate if anyone could help me out with figuring out how to start this out or complete it. Any suggestions or tips are appreciated!

Answer

val = int(input("Enter your value:"))
if val>=0 and val<=511:
    for indx,i in enumerate(bin(val)[2:].zfill(9)):
        if i=='0':
            print("H",end=" ")
        else:
            print("T",end=" ")
        if indx %3==2:
            print(" ",end="")
    print()
else:
    print("Invalid Input")