修复一个while循环

时间:2013-02-10 03:39:08

标签: python-2.7

如何修复此代码中的while循环仅在age为18时才起作用,如果小于此值则打印错误消息,并使程序在错误的邮政编码后继续运行?

这是我的代码:

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.")    

1 个答案:

答案 0 :(得分:0)

尝试类似:

age = get_age();

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

    age = get_age();
}

exit