Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 修复while循环_Python 2.7 - Fatal编程技术网

Python 2.7 修复while循环

Python 2.7 修复while循环,python-2.7,Python 2.7,如何修复此代码中的while循环,使其仅在age为18时工作,如果小于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"

如何修复此代码中的while循环,使其仅在
age
为18时工作,如果小于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.")    
尝试以下方法:

age = get_age();

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

    age = get_age();
}

exit

如果你想看到示例运行,我会运行它,但即使我输入了18岁以下的年龄,它仍然会执行zipcode块。现在你可能有缩进问题。尝试将相关部分重新键入一个单独的文件。我将它复制到一个文本编辑器中(在我的答案中进行了修改),使缩进统一,并且运行良好。这里有一个指向代码的链接,这正是我的代码,它适合我。也许你的压痕掉了。