【发布时间】:2021-01-11 17:05:33
【问题描述】:
我需要在下面的代码中添加 2 个逻辑语句。如果您未满 18 岁,我需要一个,而另一个只能让您输入 2 个字母的州。这两个语句都需要退出程序,我认为这是我感到困惑的地方。我对python很陌生,所以非常感谢任何帮助。我想我可以放 if 语句但我不知道如何让它们退出程序,我只知道如何让它们打印
print('Welcome to the US Voter Registration System')
con = input('Do You Want to Continue: Yes Or No? ')
while con == 'Yes':
name = input('Please Enter Your First Name: ')
name2 = input('Please Enter Your Last Name: ')
age = int(input('Please Enter Your Age *** YOU MUST BE 18 OR OLDER ***: '))
cit = input('Are You A US Citizen? Answer Yes or No: ')
state = input('Please Enter Your State?' 'Please only 2 letters: ')
zipc = int(input('Please Enter Your Zip Code? '))
con = input('You Are Finished Please Type Done: ')
print('NAME: ', name, name2)
print('AGE: ', age)
print('US CITIZEN:', cit)
print('STATE: ', state)
print('ZIP CODE: ', zipc)
print('Thank You For Using the US Voter Registration System ')
print('Please Check Your Mail For More Voting Information ')
【问题讨论】:
-
您可能正在寻找
sys.exit()(不要忘记在程序顶部的import sys)。虽然我建议您在深入研究任何严肃的代码项目之前阅读 Python 教程 docs.python.org/3/tutorial。 -
你可以使用 try 和 except 参考:这里 stackoverflow.com/questions/23294658/…
标签: python