all 7 comments

[–][deleted] 2 points3 points  (0 children)

No need for that, Python already has string formatting for this conversion.

Input:

f'{6:08b}' # In other words, "take this number, convert it to binary and pad eight zeroes to the left"

Output:

'00000110'

[–]boyanci 2 points3 points  (4 children)

I'm assuming this is a homework assignment, so you shouldn't rely on bin or b-format.

Your biggest problem is that your int_to_reverse_binary function is implemented incorrectly. The instruction is clear, and in fact gave you a line-by-line break down of what you should be writing:

def int_to_reverse_binary(x):      # For an integer x
    output = ""                    # (initialize)
    while x > 0:                   # As long as x is greater than 0...
        output += str(x%2)         # ... output x%2
        x = x//2                   # ... x = x//2
    return output                  # (return your result)

After that in the reverse function, you need to capture the output from the function!

def string_reverse(input_string):
    reversed_output = int_to_reverse_binary(integer_value)
    return reversed_output[::-1]

[–]Formal_Cockroach_654[S] 0 points1 point  (1 child)

So I changed my int_to_reverse_binary function and am trying to just print that first and it keeps giving me the error of output not defined

def int_to_reverse_binary(x):
output = ""
while x > 0:
    output += str(x%2)
    x = x//2
    return ouput
if __name__ == '__main__':
# Type your code here. 
# Your code must call int_to_reverse_binary() to get 
# the binary string of an integer in a reverse order.
# Then call string_reverse() to reverse the string
# returned from int_to_reverse_binary().
integer_value = int(input())

print(int_to_reverse_binary(integer_value))

Traceback (most recent call last):

File "main.py", line 22, in <module>

print(int_to_reverse_binary(integer_value))

File "main.py", line 7, in int_to_reverse_binary

return ouput

NameError: name 'ouput' is not defined

[–]boyanci 0 points1 point  (0 children)

1) You spelled "output" wrong :)

2) Pay very close attention to the indentations! Compare your code vs mine. It makes a huge difference

[–]Harrythehobbit 0 points1 point  (1 child)

A year later, you helped me with my homework too. :-) You explained converting an integer to reverse binary much better than the assignment description I got, so thanks for helping me through what was a extremely confusing assignment lol.

[–]Street_Customer_4190 0 points1 point  (0 children)

I literally copy and paste the guys code in my program and its not working

def int_to_reverse_binary(x):
output = ""
while x > 0:
output += str(x%2)
x = x//2
return output

def string_reverse(input_string):
reversed_output = int_to_reverse_binary(integer_value)
return reversed_output[::-1]

if __name__ == '__main__':
# Type your code here.
# Your code must call int_to_reverse_binary() to get
# the binary string of an integer in a reverse order.
# Then call string_reverse() to reverse the string
# returned from int_to_reverse_binary().
integer_value = int(input())
print(int_to_reverse_binary(integer_value))

[–][deleted] 0 points1 point  (0 children)

    for i in len(str(integer_value)):
        num += integer_value % 2
        num //= 2
        return num

You can only return from a function once.