我已经制作了一个程序,但是如果用户输入不是二进制格式,我想添加一个异常。我已经尝试了很多次添加异常,但我似乎无法让它工作。下面是程序代码。如果有人可以提供帮助,我将不胜感激。
import time
error=True
n=0
while n!=1:
print"***Welcome to the Bin2Dec Converter.***\n"
while error:
try:
bin2dec =raw_input("Please enter a binary number: ")
error=False
except NameError:
print"Enter a Binary number. Please try again.\n"
time.sleep(0.5)
except SyntaxError:
print"Enter a Binary number. Please try again.\n"
time.sleep(0.5)
#converts bin2dec
decnum = 0
for i in bin2dec:
decnum = decnum * 2 + int(i)
time.sleep(0.25)
print decnum, "<<This is your answer.\n" #prints output