【问题标题】:Python user input for def function用于 def 函数的 Python 用户输入
【发布时间】:2016-10-01 07:16:22
【问题描述】:

如何让用户先输入数字,然后只显示答案?

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

【问题讨论】:

  • 你可以先定义你的函数,然后再调用它。

标签: python bit-manipulation twos-complement


【解决方案1】:

我相信这就是你想要做的?

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

注意事项:

  • 您需要在使用之前定义事物,其中包括函数。
  • 缩进很重要,应该使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2014-07-15
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多