2013-02-10 99 views
0

如何修復此代碼中的while循環,以便僅在age爲18時工作,如果小於此值則打印出錯信息,並且即使在錯誤的郵政編碼之後仍然繼續執行程序?修復while循環

這裏是我的代碼:

print "VOTER ELIGIBILITY AND POLLING STATION PROGRAM" 
print 
print 
age= int(raw_input("Enter your age (Type ' 0 ' to exit program) :")) 
while age != 18: 
    print "ineligible" 
    age= int(raw_input("Enter your age (Type ' 0 ' to exit program) :")) 
    if age != 0: 
     zipcode= int(raw_input("Enter your residence's zip code:")) 

     if zipcode == 93305: 
      print "Your polling station is 123 Panorama Drive." 
     elif zipcode == 93306: 
      print "Your polling station is 6143 Fairfax Drive." 
     elif zipcode ==93307: 
      print "Your polling station is 21121 B Street." 
     elif zipcode ==93308: 
      print "Your polling station is 863 Desmond Ct." 
     elif zipcode == 93309: 
      print "Your polling station is 7332 Del Canto Ct." 
     else: 
      print "Error-unknown zip code" 

raw_input("\nRun complete.Press the Enter key to exit.")  

回答

0

嘗試類似:

age = get_age(); 

while (age != 0) { 
    if (age >= 18) ... 
    else ... 

    age = get_age(); 
} 

exit 
+0

http://www2.bakersfieldcollege.edu/cs/pwhitney/privdocs/coms_b10/COMS%20B10%20Assignment% 2003%20(polling%20station%20loop).pdf繼承人如果你希望看到樣本運行 – 44hz 2013-02-10 04:18:59

+0

我已經運行了它,但即使我鍵入18歲以下的年齡,它仍然會執行郵編塊。 – 44hz 2013-02-10 04:46:47

+0

現在您可能遇到縮進​​問題。嘗試將相關部分重新輸入到單獨的文件中。我將它複製到一個文本編輯器(在我的答案中進行了修改),使縮進統一,並且運行良好。 – par 2013-02-10 05:01:21