[英]Python else: SyntaxError: Invalid Syntax
我完全不确定为什么无论我做什么我都会不断收到此消息。 我尝试了 3 种不同的平台。 这只是代码的后半部分。 我刚刚更新了整个代码,无法弄清楚。 我知道这很简单,我必须忽略它。 请帮忙!!
第 65 行 Else: ^ SyntaxError: Invalid Syntax
# opening Statement
print("Welcome to the Python Voter Registration Application")
#continue check
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Name Collection
firstName = input("What is your First Name?\n")
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
lastName = input("what is your Last Name?\n")
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Age Verification
age = int(input("How old are you?\n"))
while age <=17 or age >= 120:
if age <= 17:
print("Sorry you are not old enough to Vote")
ageTryAgain = int(input('How old are you?\n'))
age = ageTryAgain
elif age >= 120:
print("Please enter a valid age")
ageTryAgain = int(input('How old are you?\n'))
age = ageTryAgain
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Citizen Check
citizen = input('Are you a U.S. Citizen? (Yes/No)\n')
if citizen.lower() in ['y', 'yes']:
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# State Check
state = input('Which State do you live in? ex. AL, CA, IL, ect... \n')
if state.lower() in ['al', 'ak', 'az', 'ar', 'ca', 'co', 'ct', 'de', 'dc', 'fl', 'ga', 'hi', 'id', 'il', 'in', 'ia', 'ks', 'ky', 'la','me', 'md', 'ma','mi', 'mn', 'ms', 'mo', 'mt', 'ne', 'nv', 'nh', 'nj', 'nm','ny', 'nc', 'nd', 'oh', 'ok', 'or', 'pa', 'pr', 'ri', 'sc', 'sd', 'tn', 'tx','ut', 'vt', 'va', 'wa', 'wv', 'wi', 'wy']:
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Zip Code
zipCode = int(input("Please enter Zipcode\n"))
while zipCode <= 9999 or zipCode >= 100000:
if zipCode <= 9999:
print('Please enter vaild ZipCode')
zipCodeTryAgain = int(input('Please enter Zipcode\n'))
zipCode = zipCodeTryAgain
elif zipCode >= 100000:
print('Please enter vaild ZipCode')
zipCodeTryAgain = int(input('Please enter Zipcode\n'))
zipCode = zipCodeTryAgain
print('Thank you for registering to Vote.\n Here is the information you have entered.')
print('Name (First Last): ' + firstName + " " + lastName)
print('Age: ' + str(age))
print('U.S. Citizen: Yes')
print('State: ' + state)
print('Zipcode: ' + str(zipCode))
print('Thank you for trying the Voter Registration Application.\nYour registration card should be shipped within 3 weeks')
else:
exit()
else:
print('Please enter vaild state next time')
else:
exit()
else:
print('Sorry you can not vote')
else:
exit()
Python 使用空格来构造您的代码,因此您需要正确缩进您的代码。 这是我对你想要什么的最佳猜测:
from sys import exit
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Citizen Check
citizen = input('Are you a U.S. Citizen? (Yes/No)\n')
if citizen.lower() in ['y', 'yes']:
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# State Check
state = input('Which State do you live in? ex. AL, CA, IL, ect... \n')
if state.lower() in ['al', 'ak', 'az', 'ar', 'ca', 'co', 'ct', 'de', 'dc', 'fl', 'ga', 'hi', 'id', 'il', 'in', 'ia', 'ks', 'ky', 'la','me', 'md', 'ma','mi', 'mn', 'ms', 'mo', 'mt', 'ne', 'nv', 'nh', 'nj', 'nm','ny', 'nc', 'nd', 'oh', 'ok', 'or', 'pa', 'pr', 'ri', 'sc', 'sd', 'tn', 'tx','ut', 'vt', 'va', 'wa', 'wv', 'wi', 'wy']:
preceed = input("Do you wish to continue? (Yes/No)\n")
if preceed.lower() in ['y', 'yes']:
# Zip Code
while True:
zipCode = int(input("Please enter Zipcode\n"))
if zipCode > 9999 and zipCode < 100000:
break
print('Thank you for registering to Vote.\n Here is the information you have entered.')
print('Name (First Last): ' + firstName + " " + lastName)
print('Age: ' + str(age))
print('U.S. Citizen: Yes')
print('State: ' + state)
print('Zipcode: ' + str(zipCode))
print('Thank you for trying the Voter Registration Application.\nYour registration card should be shipped within 3 weeks')
else:
print('Please enter vaild state next time')
exit()
else:
exit()
else:
print('Sorry you can not vote')
else:
exit()
考虑使用早期返回模式来避免深度嵌套的代码:
from sys import exit
citizen = input('Are you a U.S. Citizen? (Yes/No)\n')
if citizen.lower() not in ['y', 'yes']:
print('Sorry you can not vote')
exit(1)
state = input('Which State do you live in? ex. AL, CA, IL, ect... \n')
if state.lower() not in ['al', 'ak', 'az', 'ar', 'ca', 'co', 'ct', 'de', 'dc', 'fl', 'ga', 'hi', 'id', 'il', 'in', 'ia', 'ks', 'ky', 'la','me', 'md', 'ma','mi', 'mn', 'ms', 'mo', 'mt', 'ne', 'nv', 'nh', 'nj', 'nm','ny', 'nc', 'nd', 'oh', 'ok', 'or', 'pa', 'pr', 'ri', 'sc', 'sd', 'tn', 'tx','ut', 'vt', 'va', 'wa', 'wv', 'wi', 'wy']:
print('Please enter vaild state next time')
exit(1)
zipCode = int(input("Please enter Zipcode\n"))
if zipCode < 0 or zipCode > 9999:
print('Please use valid 5 digit zip code')
exit(1)
firstName = "TBD"
lastName = "TBD"
age = "TBD"
print('Thank you for registering to Vote.\n Here is the information you have entered.')
print('Name (First Last): ' + firstName + " " + lastName)
print('Age: ' + str(age))
print('U.S. Citizen: Yes')
print('State: ' + state)
print('Zipcode: ' + str(zipCode))
print('Thank you for trying the Voter Registration Application.\nYour registration card should be shipped within 3 weeks')
“ print('对不起,你不能投票') ”之前的 else 语句需要适当的缩进。 如果它给出语法错误,则您使用的 exit() 可能不会被导入。 试试 sys.exit()。 希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.