错误消息构造

时间:2013-12-12 19:03:57

标签: python python-2.7

所以,我有一个我正在上学的编程项目。我构建了项目的结构。本质上,我将这个逻辑门转换为代码,因此用户可以输入1或0并获得输出。 http://i.imgur.com/g7dYL2k.jpg 当程序运行,并且用户输入2(例如)或任何其他数字而不是0,1我想要弹出一条错误消息。我尝试了if:else:语句,但是当我这样做时,语法错误无效。

#Main instructions
A = input('Enter 0 or 1 for 1st input: ')

B = input('Enter 0 or 1 for 2nd input: ')

C = input ('Enter 0 or 1 for 3rd input: ')

print 'The logic diagram, LOGIC-1 evaluates for the input values, A, B and C to X'

print "input 1 =",A

print "input 2 =",B

print "input 3 =",C

print (A and not B) and (C or not B)

1 个答案:

答案 0 :(得分:2)

A = input('Enter 0 or 1 for 1st input: ')
if A not in (0, 1): raise Exception ('Your error message')

或者,如果您想要输入,直到它有效:

A = 42
while A not in (0, 1): 
    A = input('Enter 0 or 1 for 1st input: ')