2013-12-12 75 views
0

所以,我有一個編程項目,我正在爲學校工作。我已經構建了該項目的結構。實質上,我將這個邏輯門變成代碼,以便用戶可以輸入1或0並獲得輸出。 http://i.imgur.com/g7dYL2k.jpg 當程序運行時,並且用戶輸入2(例如)或除0之外的任何其他數字時,我想要彈出錯誤消息。我已經嘗試了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) 
+0

在未來,而不是說「我得到無效的語法錯誤,當我這樣做」,顯示實際代碼和完整的語法錯誤追溯。即使_you_無法準確理解它告訴你的是什麼,這裏的某個人可能也可以。 – abarnert

回答

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: ') 
+0

謝謝!我不知道異常功能。感謝你的幫助! – user3096403

+0

很高興幫助。 'Exception'不是一個函數,而是一個類。 – Hyperboreus