如何创建一个简单的循环以返回问题?

时间:2019-02-25 01:02:32

标签: python python-3.x python-idle

我正在尝试创建一个循环,询问用户年龄和邮政编码,一旦返回投票站,它应循环返回并再次询问年龄和邮政编码。到目前为止,我的代码只要求两者,返回桩号,然后完成。

print("VOTER ELIGIBILITY AND POLLING STATION PROGRAM\n\n")
ageMinimum = 18
exitVal = 0
zipCode = 0
zipCodeTemp = 0
age = int(input("Enter your age (Type '0' to exit the program): "))

while age < ageMinimum and age != exitVal:
    print("You are not eligible to vote")
    age = int(input("Enter your age (Type '0' to exit the program): "))

if age == exitVal:
    input("\nRun complete. Press the Enter key to exit.")

if age != exitVal:
    zipCode = int(input("Enter your residence's Zip Code: "))
if age != exitVal and zipCode == ...:
    print("Your polling station is ...")
elif age != exitVal and zipCode == ...:
    print("Your polling station is ...")
elif age != exitVal and zipCode == ...:
    print("Your polling station is ...")
elif age != exitVal and zipCode == ...:
    print(" Your polling station is ...")
elif age != exitVal and zipCode == ...:
    print("Your polling station is ...")
elif zipCode != zipCodeTemp:
    print("Error – unknown zip code")

1 个答案:

答案 0 :(得分:1)

这应该可以解决您遇到的任何问题:

print("VOTER ELIGIBILITY AND POLLING STATION PROGRAM\n\n")
ageMinimum = 18
exitVal = 0
zipCode = 0
zipCodeTemp = 0
while True:
    age = int(input("Enter your age (Type '0' to exit the program): "))

    while age < ageMinimum and age != exitVal:
        print("You are not eligible to vote")
    if age == exitVal:
        input("\nRun complete. Press the Enter key to exit.")
        exit()
    if age != exitVal:
        zipCode = int(input("Enter your residence's Zip Code: "))
    if age != exitVal and zipCode == ...:
        print("Your polling station is ...")
    elif age != exitVal and zipCode == ...:
        print("Your polling station is ...")
    elif age != exitVal and zipCode == ...:
        print("Your polling station is ...")
    elif age != exitVal and zipCode == ...:
        print(" Your polling station is ...")
    elif age != exitVal and zipCode == ...:
        print("Your polling station is ...")
    elif zipCode != zipCodeTemp:
        print("Error – unknown zip code")
    else:
        print("Some sort of error has occured.")