Joseph Joseph - 1 month ago 15
Python Question

Try- except ValueError loop

def enterNumber():
number = input("Please enter a number to convert to binary. ")
while True:
try:
int(number)
convertDenary()
except ValueError:
enterNumber()
def convertDenary():
binaryNumber = ['','','','','','','','']
print(enterNumber())
if enterNumber() > 128:
enterNumber() - 128
binaryNumber[0] == 1
enterNumber()


The
Try- Except ValueError
does loop as I intend it to however, it won't break. I've tried adding in
break
under the int(number), removing the
while True:
and added in the
convertDenary()
to see if it will force the subroutine to stop and start the other but it still doesn't work.

I get an infinite loop of "Please enter a number to convert to binary."
Any ideas?

Answer
def convertToBinary(number):
   if number > 1:
       convertToBinary(number//2)
   elif number<1:
       enterNumber()

   print(number % 2,end = '')

def enterNumber():
    number = (input("Please enter a number to convert to binary : "))
    try:
        convertToBinary(int(number))
    except Exception as e:
        print(e)

enterNumber()