1

So I've currently got a function that allows the user to give an integer and can distinguish between an integer and an exiting string, as well as a random on. However I'm struggling with making another function that will convert this integer into a binary number, I know that'll require floored division and modulo, but I'm relatively lost here.

def getInt(question):
  return (question)
  print("Welcome to Binary Printer")
  print('Enter exit to quit at any time.')
  i=True
  while i:
    question=input("Enter a Positive Int:\n")
    try:
      if(question=="Exit") or (question=="exit"):
        i=False
        print("")
      else:
        integer_check=int(question)
  except ValueError:
    print("Not a Number.")

def binaryStr(num,bits):
  num=getInt(question)
  bits=int(input("Number of Bits:\n"))
  for num in range(0,255):
    one_or_zero=num%2
    if one_or_zero>0:
        one_or_zero//2
    else:
        return one_or_zero


def main():
    getInt(question)
    binaryStr(num,bits)


main()
PilouPili
  • 2,601
  • 2
  • 17
  • 31
r_canopies
  • 11
  • 3

2 Answers2

0

this is already built into python format strings

{key:{FILL_VALUE}{WIDTH}b} the b stands for binary :P

print("{number:0{n_bits}b}".format(number=23,n_bits=16))
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0
def int_to_binary(your_number,number_of_bit):
    return f'{your_number:0{number_of_bit}b}'.format(6)
Sari Masri
  • 206
  • 1
  • 10