Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写一个Python程序,将二进制序列(仅0和1)转换为十进制整数_Python - Fatal编程技术网

编写一个Python程序,将二进制序列(仅0和1)转换为十进制整数

编写一个Python程序,将二进制序列(仅0和1)转换为十进制整数,python,Python,要求: 定义一个函数decimals,该函数将二进制序列转换为十进制整数并返回该整数 要求用户输入一个二进制序列x,它是由逗号分隔的4位二进制数组成的序列,并调用小数x。显示返回的结果 3.样本输出为: Please input a binary sequence (comma separated every 4 digits):10,1011 The decimal integer of 10,1011 is: 43 我尝试编写的程序是: def decimal(s): num =

要求:

定义一个函数decimals,该函数将二进制序列转换为十进制整数并返回该整数

要求用户输入一个二进制序列x,它是由逗号分隔的4位二进制数组成的序列,并调用小数x。显示返回的结果

3.样本输出为:

Please input a binary sequence (comma separated every 4 digits):10,1011
The decimal integer of 10,1011 is: 43
我尝试编写的程序是:


def decimal(s):
    num = s
    dec_value = 0; 


    base1 = 1; 

    len1 = len(num); 
    for x in range(len1 - 1, -1, -1): 
        if (num[x] == '1'):      
            dec_value += base1; 
        base1 = base1 * 2; 

    return dec_value

num=list(input("Please input a binary sequence (comma separated every 4 digits):").split(','))


print("The decimal integer of the number is:", (decimal(num)))

在本例中,十进制整数返回为0,但应为43。

尝试将二进制字符串传入:

def seethat(hm):
  j=1
  for c in range(0, len(hm)):
    if hm[c] == '0':
      j<<=1
    else:
      j = (j<<1)-1
  return j-1

只需对代码进行少量更改,就可以使其正常工作

我认为问题在于您在字符串上使用了拆分而不是替换:

def decimal(s):
    num = s
    dec_value = 0; 


    base1 = 1; 

    len1 = len(num); 
    for x in range(len1 - 1, -1, -1): 
        if (num[x] == '1'):      
            dec_value += base1; 
        base1 = base1 * 2; 

    return dec_value

num=list(input("Please input a binary sequence (comma separated every 4 digits):").replace(',',''))
# only change to code is using .replace instead of .split
print(num)

print("The decimal integer of the number is:", (decimal(num)))
split使用您的示例为您提供了一个[1011011]列表,因此列表中的项目被您的代码忽略x永远不等于1-在本例中,它是10或1011


replace为您提供了一个列表[1,0,1,0,1,1],它与您编写的函数一起工作。

那么,您到底得到了什么错误?如果二进制数字中有逗号,则答案为零。提示:您没有掩饰最初的问题,您以前对它所做的更改已被修改。请不要通过破坏您的帖子为他人做更多的工作。通过在Stack Exchange SE网络上发布,您已根据a授予SE分发内容的不可撤销权利,即无论您未来的选择如何。根据SE政策,分发非故意破坏版本。因此,任何故意破坏行为都将恢复原状。请参阅:。如果允许删除,在帖子下方左侧有一个删除按钮,但它仅在浏览器中,而不是移动应用程序中。谢谢您的支持answering@chunglinglam你们最好点击答案左边的复选标记,而不是评论它
def decimal(s):
    num = s
    dec_value = 0; 


    base1 = 1; 

    len1 = len(num); 
    for x in range(len1 - 1, -1, -1): 
        if (num[x] == '1'):      
            dec_value += base1; 
        base1 = base1 * 2; 

    return dec_value

num=list(input("Please input a binary sequence (comma separated every 4 digits):").replace(',',''))
# only change to code is using .replace instead of .split
print(num)

print("The decimal integer of the number is:", (decimal(num)))