Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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二进制数到十进制简单转换器_Python_For Loop_Binary_Decimal - Fatal编程技术网

Python二进制数到十进制简单转换器

Python二进制数到十进制简单转换器,python,for-loop,binary,decimal,Python,For Loop,Binary,Decimal,基本上,我只是想用简单的命令为我的GCSE制作一个二进制到十进制的转换器。我应该如何安排for循环,使其一直运行,直到所有的数字都被转换。此外,如何打印所有计算的加法。我将介绍一种简单的方法: userinput=(input ("Please Enter Your Binary Number")) lengh=len (userinput) x=lengh-1 num=(2**x) char2=-lengh for i in range(lengh) int(userinput[

基本上,我只是想用简单的命令为我的GCSE制作一个二进制到十进制的转换器。我应该如何安排for循环,使其一直运行,直到所有的数字都被转换。此外,如何打印所有计算的加法。

我将介绍一种简单的方法:

userinput=(input ("Please Enter Your Binary Number"))


lengh=len (userinput)

x=lengh-1
num=(2**x)


char2=-lengh

for i in range(lengh)

int(userinput[char2])*num

x=x-1
char2=char2+1
把绳子倒过来,这样我们就可以从右向左操作了。这可以通过切片s[:-1]完成。然后,我们可以使用枚举来有效地获得基于索引的幂

s = '101101101'
因此,遍历for循环的前几个迭代

total = 0
for index, value in enumerate(s[::-1]):
    total += int(value) * 2**index

>>> total
365
total += 1 * 2**0
total += 0 * 2**1
total += 1 * 2**2
total += 1 * 2**3
... etc