简单Python:关于代码结构
Simple Python: about structure of code
print ("This program reads an unsigned binary number of arbitrary length
\nand tries to compute the decimal equivalent. The program reports an error
\nif the input number contains characters other than 0 and 1.")
dec = 0
bin = 0
factor = 1
print ("Enter the binary number: ")
bin = int(input())
while(bin > 0):
if((bin % 10) == True):
dec += factor
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
print ("I think your binary number in decimal form is: " ,dec)
这是我的程序代码,应该将用户的二进制数转换为十进制数。它工作正常,但我正在尝试添加一个 "else" 语句,如果用户输入 0 或 1 以外的数字,它将打印 "unrecognized bit"。它可以工作,但是程序打印 "Unrecognized bit" 即使用户只输入了 0 和 1。这不应该发生。另请参阅相关图片。我已经输入 12343 来测试程序,它显示无法识别的位,这很好,但它也采用该数字中的“1”并将其转换为 16,这不应该发生,它应该只是说无法识别的位。我认为这两个问题很容易解决,但我不确定。谢谢!
picture
是缩进的问题。无论 if else 语句说什么,您的最后一行都会打印出来。当遇到无法识别的符号时,您还想退出 while 循环。现在它打印 else 语句但一直循环。
这是一个修复,我添加了一个中断条件以退出 else,然后添加了一个布尔值以查看我们是否可以输出最后一个打印语句:
print ("This program reads an unsigned binary number of arbitrary length tries to compute the decimal equivalent. The program reports an error if the input number contains characters other than 0 and 1.")
dec = 0
bin = 0
factor = 1
success = 1
print ("Enter the binary number: ")
bin = int(input())
while(bin > 0):
if((bin % 10) == True):
dec += factor
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
success = 0
break
if(success):
print ("I think your binary number in decimal form is: " ,dec)
两个问题:
仅当您的二进制输入的余数为 1 时才满足条件,因为 0 == False
的计算结果为 True
。也就是说,如果您的数字以零结尾(或者实际上 包含 一个零),(bin%10) == True
将计算为 False
(旁注:有其实不用加右边的== True
)。
无论您查看的数字是 1
还是 0
,您都在将 factor
添加到 dec
;如果遇到零,则不应添加。
因此您的代码应如下所示:
while(bin > 0):
remainder = (bin % 10)
if remainder in (0,1):
dec += factor * remainder
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
break
您需要测试您的输入以确保它是合法的。您可以使用正则表达式来执行此操作。将 bin = int(input())
替换为:
import re
inp = input()
if re.match('.*[^01]+.*', inp) is not None:
print("illegal character in input, must be ones and/or zeros")
exit()
bin = int(inp)
如果输入不包含 1 或 0,这将打印非法字符消息。
print ("This program reads an unsigned binary number of arbitrary length
\nand tries to compute the decimal equivalent. The program reports an error
\nif the input number contains characters other than 0 and 1.")
dec = 0
bin = 0
factor = 1
print ("Enter the binary number: ")
bin = int(input())
while(bin > 0):
if((bin % 10) == True):
dec += factor
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
print ("I think your binary number in decimal form is: " ,dec)
这是我的程序代码,应该将用户的二进制数转换为十进制数。它工作正常,但我正在尝试添加一个 "else" 语句,如果用户输入 0 或 1 以外的数字,它将打印 "unrecognized bit"。它可以工作,但是程序打印 "Unrecognized bit" 即使用户只输入了 0 和 1。这不应该发生。另请参阅相关图片。我已经输入 12343 来测试程序,它显示无法识别的位,这很好,但它也采用该数字中的“1”并将其转换为 16,这不应该发生,它应该只是说无法识别的位。我认为这两个问题很容易解决,但我不确定。谢谢!
picture
是缩进的问题。无论 if else 语句说什么,您的最后一行都会打印出来。当遇到无法识别的符号时,您还想退出 while 循环。现在它打印 else 语句但一直循环。
这是一个修复,我添加了一个中断条件以退出 else,然后添加了一个布尔值以查看我们是否可以输出最后一个打印语句:
print ("This program reads an unsigned binary number of arbitrary length tries to compute the decimal equivalent. The program reports an error if the input number contains characters other than 0 and 1.")
dec = 0
bin = 0
factor = 1
success = 1
print ("Enter the binary number: ")
bin = int(input())
while(bin > 0):
if((bin % 10) == True):
dec += factor
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
success = 0
break
if(success):
print ("I think your binary number in decimal form is: " ,dec)
两个问题:
仅当您的二进制输入的余数为 1 时才满足条件,因为
0 == False
的计算结果为True
。也就是说,如果您的数字以零结尾(或者实际上 包含 一个零),(bin%10) == True
将计算为False
(旁注:有其实不用加右边的== True
)。无论您查看的数字是
1
还是0
,您都在将factor
添加到dec
;如果遇到零,则不应添加。
因此您的代码应如下所示:
while(bin > 0):
remainder = (bin % 10)
if remainder in (0,1):
dec += factor * remainder
bin //= 10
factor = factor * 2
else:
print("unrecognized bit:")
break
您需要测试您的输入以确保它是合法的。您可以使用正则表达式来执行此操作。将 bin = int(input())
替换为:
import re
inp = input()
if re.match('.*[^01]+.*', inp) is not None:
print("illegal character in input, must be ones and/or zeros")
exit()
bin = int(inp)
如果输入不包含 1 或 0,这将打印非法字符消息。