0

So I am trying to chop the user input of 1s and 0s to print out their decimal counterpart, but no matter what when I enter: "10000000111111110000001100001111" I get 256 for the second set of 8 numbers instead of 255. I am stumped. I get 128 for value 1, but 256 for value 2. I don't think I understand what (number[6:10]) is even doing.

def convert(bits):
    answer = 0
    keeper = 128
    for i in bits:
        if(i == '1'):
            answer = answer + keeper
            keeper / 2
    return answer





number = str(input("Please enter a 32-bit integer: "))


value1 = convert(number[0:7])
value2 = convert(number[6:10])

print(value1, value2)
Catz
  • 11
  • 5
  • 1
    change `keeper / 2` to `keeper /= 2` – sam-pyt Dec 09 '17 at 01:51
  • Thank you! It now prints "255.0", how can I get rid of the ".0"? – Catz Dec 09 '17 at 02:01
  • `keeper //= 2` will do the trick – sam-pyt Dec 09 '17 at 02:02
  • Thank you again! So with this method, If i Continue with a value3 and try and grab the next 8 pieces of string it gives a wrong number no matter what. value1 = convert(number[0:7]) value2 = convert(number[8:16]) value3 = convert(number[16:24]) Value 3 should only be 3, not 192 like it comes out:/ any idea what is going on with this thing? I am just trying to take the 32 bit value and give back 128.255.3.15. – Catz Dec 09 '17 at 02:09

1 Answers1

0

The value of keeper isn't being updated when you do keeper / 2.

You have to catch the value of that statement and assign it to keeper:

keeper = keeper // 2 or keeper //= 2

Since you want to split the user's input into 8 bit blocks, you should also change the indexes in your slicing. In your case you would want:

value1 = convert(number[0:8])
value2 = convert(number[8:16])

I don't think I understand what (number[6:10]) is even doing.

number is being set to a string of the number the user inputs, in your case "10000000111111110000001100001111".

By saying number[6:10], you're slicing the string meaning you're getting a specific piece of the string. The first number 6 is the starting point and 10 is the endpoint -- but note that 10 is exclusive meaning you will get the string starting at index 6 and ending at but not including the character at index 10. Also note that the very first index of the string is 0, and not 1. There are some helpful examples here.

Joseph
  • 74
  • 4
  • You're trying to convert 8 digit blocks from binary into decimal. So you should only be adding `128` to `answer` if the **first** digit in the block is a 1, otherwise you need to update the value of `keeper`. I think [this picture](https://www.wikihow.com/images/thumb/e/e2/Convert-from-Binary-to-Decimal-Step-3-Version-2.jpg/aid5975-v4-728px-Convert-from-Binary-to-Decimal-Step-3-Version-2.jpg) would be helpful. As you are traversing your string, you need to be updating the value of `keeper` even when the digit is not 1. So you should move `keeper //= 2` outside the if (reduce indent). – Joseph Dec 09 '17 at 02:21
  • Thank you so much! My last issue was "Keeper" not halving when encountering 0s as it should have! Finally figured it out! Thank you again! – Catz Dec 09 '17 at 02:22
  • Could I bother you for one last thing? When I run: print(value1,".",value2,".",value3,".",value4) It prints out: 128 . 255 . 3 . 15, how do i get rid of all those spaces? – Catz Dec 09 '17 at 02:29
  • You have to use string concatenation or formatting if you don't want spaces between things. You can't use print(`foo` , `bar` , `baz`) as the commas are what gives you the spaces. See [here](https://stackoverflow.com/questions/28669459/how-to-print-variables-without-spaces-between-values) – Joseph Dec 09 '17 at 02:34
  • Thank you so much for your help! – Catz Dec 09 '17 at 02:37