[英]Python else: SyntaxError: Invalid Syntax
I'm not sure at all why I keep getting this message no matter what i do.我完全不确定为什么无论我做什么我都会不断收到此消息。 I've tried in 3 different plateforms.
我尝试了 3 种不同的平台。 This is just the last half of the code.
这只是代码的后半部分。 I just updated with the whole code and can't figure it out.
我刚刚更新了整个代码,无法弄清楚。 I know it's something simple, and i have to be overlooking it.
我知道这很简单,我必须忽略它。 PLEASE HELP!!
请帮忙!!
Line 65 Else: ^ SyntaxError: Invalid Syntax第 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 uses whitespace to structure your code, so you need to indent your code correctly. Python 使用空格来构造您的代码,因此您需要正确缩进您的代码。 Here my best guess as to what you want:
这是我对你想要什么的最佳猜测:
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()
Consider using the early return pattern to avoid deeply nested code:考虑使用早期返回模式来避免深度嵌套的代码:
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')
else statement before " print('Sorry you can not vote') " need the proper indentation. “ print('对不起,你不能投票') ”之前的 else 语句需要适当的缩进。 and if it's giving syntax error, the exit() you using might be not imported.
如果它给出语法错误,则您使用的 exit() 可能不会被导入。 try sys.exit().
试试 sys.exit()。 Hope this will be helpful
希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.