0

This is all the further i've gotten.

import math

num_to_convert = int(input("Please enter any intger from 1 and 100:"))

while num_to_convert < 1 or num_to_convert > 100:
    num_to_convert = int(input("Sorry that's not an integer from 1 to 100, try again:"))

else:
    print("I'm lost!")

I found this but I don't understand whats going on. Maybe some explanation of what's going on would help.

def decimalToBinary(n):
 
    if(n > 1):
        # divide with integral result
        # (discard remainder)
        decimalToBinary(n//2)
 
     
    print(n%2, end=' ')
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    If you don't understand it, [then don't use it](https://stackoverflow.com/questions/699866/python-int-to-binary-string) – OneCricketeer Sep 03 '21 at 20:39
  • `def decimalToBinary(n): return int(bin(n)[2:])` – 001 Sep 03 '21 at 20:39
  • As far as understanding the code you found, add some `print` statements and run it and watch the value of `n`. – 001 Sep 03 '21 at 20:41
  • What part of it don't you understand? `n%2` returns the least significant binary digit. – Barmar Sep 03 '21 at 20:41
  • And `n//2` divides by 2. – Barmar Sep 03 '21 at 20:41
  • So, what's the actual question? How to convert, or what that _specific method_ does? – OneCricketeer Sep 03 '21 at 20:42
  • Even better than adding `print` statements is to pretend you're the computer. Write the variables down on paper, and execute the code by hand to see how they change. – Barmar Sep 03 '21 at 20:44
  • It's supposed to be integer to binary. Write a program that takes an integer from 1 to 100 and converts it to binary. – Drewcifer25 Sep 03 '21 at 21:14
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 07 '21 at 23:16

3 Answers3

0

It seems like you want to convert an integer which is not a decimal to binary from your code i would write

while True:
    try:
        value1=input("Integer you want to convert to binary:  ")
        binaryvalue=(bin(int(value1)))
        print (binaryvalue[2:])
    except:
        print("I did not understand that")
        pass
0
Valuetoconvert=int(input("Number to convert:   "))
u = format(Valuetoconvert, "08b")
print(u)

Try this then

0

See Below:

def toBin(n):
    if n < 2:
        return str(n)
    else:
        if n % 2 == 0:
            return toBin(n//2) + "0"
        else:
            return toBin(n//2) + "1"

Explanation:

This is my sollution which works similar to yours. I hope you know what recursion is otherwise this is going to be difficult to understand. Anyway the algorithm is to devide the number repeatedly by 2 until the number is smaller than 2 cause then you have the sollution right away(base case).

When the current number is greater than 2 you check wether it is divisible by 2. If it is even you append a 0 to your string else append a 1. You can try this out on paper to better understand it.