简体   繁体   中英

Python user input for def function

How to do a number input by user first then only display the answer?

a = int(raw_input("Enter number: "),twoscomp)

def tobin(x, count = 8):
    return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, -1)))

def twoscomp(bin_str):
    return tobin(-int(bin_str,2),len(bin_str))

print a

I believe this is what you're trying to do?

def tobin(x, count = 8):
    return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, -1)))

def twoscomp(bin_str):
    return tobin(-int(bin_str,2),len(bin_str))

a = twoscomp(raw_input("Enter number: "))

print a

Things to note:

  • You need to define things before you use them, and that includes functions.
  • Indentation is important, and you should use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM