如何正确使用while和if语句

问题描述 投票:0回答:1

我正在尝试练习while语句,如果要声明,有人可以向我解释我做错了什么吗?我将提供与我自己的代码相匹配的代码,仅提供我的邮政编码示例以及投票站的位置:

print ("VOTER ELIGIBILITY AND POLLING STATION PROGRAM\n")
ageMin = 18
end = 0
zipCode = 0
usrAge = int(input("\nEnter your age (Type '0' to exit program): "))
while usrAge < ageMin and usrAge != end:
    print("YOU ARE INELIGIBLE TO VOTE")
    usrAge = int(input("\nEnter your age (Type '0' to exit program): "))
if usrAge == end:
    input("\nRun complete. Press the Enter key to exit.")
while usrAge != end:
    zipCode = int(input("\nEnter your residence's zip code: "))
    usrAge = int(input("\nEnter your age (Type '0' to exit program): "))
    input("\nRun complete. Press the Enter key to exit.")
if usrAge != end and zipCode == "":
    print("Your polling station is ")
if usrAge != end and zipCode == "":
            print("Your polling station is .")
elif usrAge != end and zipCode == "":
            print("Your polling station is .")
elif usrAge != end and zipCode == "":
            print("Your polling station is .")
elif usrAge != end and zipCode == "":
            print("Your polling station is .")
else:
            print("Error – unknown zip code")

该脚本可以运行,但是由于某种原因它不会显示正确的轮​​询地址,即使我不希望它也直接进入else语句,并且我也在尝试循环usrAge语句,因此用户可以根据需要随时进行提示。

示例:

VOTER ELIGIBILITY AND POLLING STATION PROGRAM


Enter your age (Type '0' to exit program): 19

Enter your residence's zip code: myzipCode

Enter your age (Type '0' to exit program): 0

Run complete. Press the Enter key to exit.
Error – unknown zip code
python-3.x
1个回答
0
投票

if语句中的每一个都表示usrAge != end,因为while语句必须在相等时结束,所以永远不会成立

© www.soinside.com 2019 - 2024. All rights reserved.