3
\$\begingroup\$

Am supposed to capture user input as an integer, convert to a binary, reverse the binary equivalent and convert it to an integer.Am getting the right output but someone says the solution is wrong. Where is the problem?

x  = 0

while True:

    try:
        x = int(raw_input('input a decimal number \t'))


        if x in xrange(1,1000000001):

            y = bin(x)

            rev = y[2:]

        print("the reverse binary soln for the above is %d") %(int('0b'+rev[::-1],2))       
            break

    except ValueError:
        print("Please input an integer, that's not an integer")
        continue
\$\endgroup\$
2
  • 2
    \$\begingroup\$ If you are asking us to find an error in your code, then I'm afraid your question if off topic on Code Review. This is site is for reviewing code that you think is correct. \$\endgroup\$ Commented Jan 27, 2013 at 22:28
  • 1
    \$\begingroup\$ I think ist's correct since it gives the required output, I just want to know if there's something I can do to make it work better \$\endgroup\$ Commented Jan 27, 2013 at 22:55

1 Answer 1

5
\$\begingroup\$
x  = 0

There is no point in doing this. You just replace it anyways

while True:

    try:
        x = int(raw_input('input a decimal number \t'))


        if x in xrange(1,1000000001):

Probably better to use if 1 <= x <= 100000000001: although I'm really not sure why you are doing this check. Also, you should probably explain to the user that you've reject the number.

            y = bin(x)

            rev = y[2:]

I'd use reversed = bin(x)[:1::-1] rather then splitting it out across the tree lines.

        print("the reverse binary soln for the above is %d") %(int('0b'+rev[::-1],2))    

I'd convert the number before the print to seperate output from the actual math.

            break

    except ValueError:
        print("Please input an integer, that's not an integer")
        continue

This continue does nothing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.