Why doesnt my python program work? -


ans = true while ans:     print ("""do want 1.convert decimal binary 2 or convert binary decimal """)     ans = input("which program run? ")     print ans      if ans==1:        binarynumber = int(input("please input valid intager "))        answer = ''        in range(8):            ## loop dom conversion intager binary            x = binarynumber % 2             binarynumber = binarynumber // 2            answer += str(x)         answer = answer [::-1]        print(answer)      elif ans==2:         input = input("enter binary number 8 digits long: ")         decimalnumber = input[::-1]         count = 1          answer = 0          bit in decimalnumber:             if bit == "1" :                 answer += count                 count *= 2             if bit == "0" :                 answer = count                 count *= 2                 print ("number")             elif ans !="":                 print("\n not valid choice try again") 

this code convert binary number decimal number, when run it comes traceback error message, have thurally reviewed code , should work doesnt. ignore first half, decimal binary converter , works perfectly.

there @ least several problems:

  1. the variable name input bad, you'd define function read_input_as_int make code more readable

i think overthink problem.

to convert binary decimal, can call

try:    number = input('please input decimal number')    print int(number, 2) except:    print 'not valid decimal number' 

to convert decimal binary, can simple call

try    number = input('please input binary number')    print bin(number)[2:] except:    print 'not valid binary number' 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -