My code seems to break in line 2 at int(number), i don’t know why though?

Viewed 36

Please help. I have the follow exercise but I can't get my code to work.


For every positive total number n you can determine a unique code consisting of two numbers m and p. That works like this:

  • Step 1: Write n as a number in the binary system, as a binary number b. See below for explanation.
  • Step 2: Write b backwards as a binary number a.
  • Step 3: The number m is the number of zeros with number one starting.
  • Step 4: You get the number by writing the binary number as a number in the decimal system.

For example, suppose n = 202.

  1. Binary you can write that as 1100010.
  2. If you write that backwards, you get 01010011.
  3. That number starts with 1 times a 0.
  4. If you omit that you get 1010011. Write that again in the decimal system and you get 83.

The AB code of 202 is then the pair 1 and 83. Write a program that takes a number n in the least of standard input. 0 < n < 1000000000 applies.
The program writes two lines to standard output. On the first line is the number m and on the second the number p.

Example:
Input: 202
Output: 1 83


number = input()
a = f'{​​​​​​int(number):0b}​​​​​​'
a = str(a)
code = []
zeroes = 0
for i in a:
  code.append(i)
code.reverse()
while True:
  if code[0] == "0":
    code.pop(0)
    zeroes = zeroes + 1 
else:
  break 
  x = ''
for i in range(len(code)):
  x = x + code[i]
newnum = int(x ,2)
print(str(zeroes))
print(newnum)
1 Answers

I am not sure but is the fString statement correct? Or should it be like:

f'int({number}):0b'

Related