new = 0
binary=input ("Enter binary:")
length = len(binary)
for x in range (0,3):
if length<9 and all (c in "01" for c in binary):
for digits in binary:
new = new*2 + int(digits)
print ("The binary numbers {0} converted into decimal is {1}".format(binary,new))
break
else:
binary=input ("Try again. Enter binary (max. 8):")
print ("The programme will end")
代码运行良好并输出初始3输入的十进制格式,但最后,当输入正确时,它仍然输出&#34;程序将结束&#34;而不是小数。
编辑: 感谢您的帮助,我已经解决了问题
答案 0 :(得分:4)
....最初的3个输入,但是最后的
我在你的代码中看到以下循环
for x in range (0,3):
请注意,function range
is "half-open"表示它包含第一个值,但不包括最后一个值。换句话说,x
将采用值
0
1
2
如果您还想包含所需的值3
for x in range (0,4):