Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 使用while循环的二进制到十进制转换器_Python - Fatal编程技术网

Python 使用while循环的二进制到十进制转换器

Python 使用while循环的二进制到十进制转换器,python,Python,在计算十进制等价物时,我无法用正确的幂乘以2 b=str(input("Enter binary number here ")) a=1 c=1 for i in (b): while i==0: a=0 while i==1: c=1*(2**b(i)) print(c+a) 我只得到2作为输出检查二进制数字是否为0或1不是必需的,因为乘以0将返回0 bin_no=input('Enter a binary number:')#input默认情

在计算十进制等价物时,我无法用正确的幂乘以2

b=str(input("Enter binary number here "))
a=1
c=1
for i in (b):
    while i==0:
        a=0
    while i==1:
        c=1*(2**b(i))
print(c+a)

我只得到2作为输出

检查二进制数字是否为
0
1
不是必需的,因为乘以0将返回0

bin_no=input('Enter a binary number:')#input默认情况下以str的形式返回输入
基数=2
place_val=0
十进制=0
对于bin_no[:-1]:#中的数字,以相反的方式遍历这些数字
小数+=整数(位数)*基数**位值
place_val+=1
打印(f'Decimal of{bin_no}是{Decimal})
输出:

Enter a binary number: 101101
Decimal of 101101 is 45
45
或者可以使用
int
将二进制数转换为十进制数

b='101101'
x=int(b,2)
打印(x)
输出:

Enter a binary number: 101101
Decimal of 101101 is 45
45

这里有相当多的错误(对于刚开始工作的人来说并不少见),所以我将首先解释一下什么是错误的。下面是与您的代码内联的注释:

b=str(input("Enter binary number here "))

a=1
c=1

# No need for parens here, it doesn't add anything.
for i in (b):
    # Two issues: i is never 0 (the integer).
    # It could be '0' (a string containing the ASCII digit 0).
    # Also, even if you fix the conditional (i == '0'), the loop
    # would never end because we do nothing to change i inside
    # the loop.
    while i==0:
        a=0

    # Similar problem.  i can never be the integer 1 here, and
    # the loop condition is still a problem.  You probably
    # really wanted 'if' instead.
    while i==1:
        # Not entirely sure what you were going for here.  It
        # looked like you got the idea that i would be 0 or 1
        # (and now you know it's actually '0' or '1'), but
        # then you do: b(i), which I can't figure out.  I think
        # you were trying to do something like 2**i, where i is
        # the position of the digit (which it's not).
        #
        # Either way, it's not doing what you expect.  The
        # syntax: name(arg) is really a function call.  So
        # you're trying to call your string with the current
        # digit, which Python cannot do and will throw an
        # exception.
        c=1*(2**b(i))

# In the end, you're not really adding things up as you go
# along, so the end result will never actually be the value.
print(c+a)
现在我们知道了问题所在,我们有了一些选择。对于示例,我们将假定
b
“1011”
,因为很容易看出我们的错误。如果最左边的数字是最高有效位,则该值应为
11
。如果最右边的数字是有效的,那么该值将是13

选择1 选项3(您的方法,反向)
就个人而言,我最喜欢选项1,因为它简洁、易读、易懂;x=int(b,2);打印(x);-->45在您的代码中,
i
何时等于
0
1
?(不是)另外,请注意,您正在使用
循环,如果条件为真,它们永远不会结束,因为条件永远不会改变(循环中的内容不会影响条件)。最后,什么是
b(i)
?我相信你可能不明白b中的I给了你什么。。。它将获取从
input()
行键入的每个字符——不涉及数字。:-)如果我们这样做:我的任务是使用嵌套循环来编写代码,而我刚刚开始,所以我在理解移位运算符和“枚举”函数时遇到了一些问题。我尝试过使用以下方法:bin=(输入(“此处输入二进制数”))val=0 n=len(bin)表示范围(n,0,-1):对于数字(bin)中的j:如果j==1:val+=j*(I**2)print(val)bin=(输入(“此处输入二进制数”))val=0 n=len(bin)表示范围(n-1,0,-1):打印(I)表示(bin)中的数字:如果数字==1:val+=digit*(i**2)打印(val)为什么不打印work@SatwikKumar在Python中,
for
实际上是一个“for-each”样式的循环。这意味着它不计算一系列的数字,而是给你一个容器中的每个项目。当你把你的字符串包起来,而不是仅仅得到一个字母,你会得到一个索引和一个字母。描述
@SatwikKumar的最佳方法是它不起作用,因为字符串数字“1”与整数1不同。在解释器中尝试:
1==“1”
将为您提供
False
。Python不会自动将看起来像数字的字符串转换为整数。在上面的代码中,我假设您得到了
0
作为答案。原因是
digit==1
应该是
digit==“1”
(注意后面的引号,它们很重要)。如果您的目标是将其作为嵌套循环来完成,那么您需要更多地考虑如何实现这一点。提示:你能把2**i的乘法转换成循环吗?
val = 0
for i, digit in enumerate(reversed(b)):
   # Here i will count up as we iterate through the digits.
   # This will compute the value the way you expect, treating
   # rightmost digits as less significant.  This works because
   # we reversed the string before computing the answer.
   if digit == '1':
       val += 2**i

print(val)
# 11