[英]What to do from stopping the program from exiting in Python before if command
I used the following code for building a simple Python program which gets input from user. 我使用以下代码构建一个从用户获取输入的简单Python程序。
Name = raw_input('Enter your name: ')
Age = raw_input('Enter you age:')
Qualifications = raw_input('Enter you Qualification(s):')
A = raw_input()
print "Hello" +'.'+ "Your Name is " + Name + ". " + "Your Age is " + Age + ". " + "Your Qualification(s) is/are:" + Qualifications + "."
print "If the above data is correct, type yes. If not, type no. Typing no will restart he program and make you write the form again." + A
if A in ['y', 'Y', 'yes', 'Yes', 'YES']:
print 'Thanks for your submission'
if A in ['No' , 'no' , 'NO' ,'n' , 'N']:
reload()
The program finishes before the if command. 程序在if命令之前完成。
If you give anything but ['y', 'Y', 'yes', 'Yes', 'YES']
or ['No' , 'no' , 'NO' ,'n' , 'N']
, your program will finish and not execute any of the statements in their respective if
-clauses. 除了
['y', 'Y', 'yes', 'Yes', 'YES']
或['No' , 'no' , 'NO' ,'n' , 'N']
,程序将完成并且不执行其各自if
-clauses中的任何语句。
The reload()
function will not do what you expect. reload()
函数不会按预期执行。 It's used to reload a module and should be used inside the interpreter. 它用于重新加载模块,应该在解释器中使用。 It also requires a previously imported
module
as it's argument, calling it without will raise a TypeError
. 它还需要先前导入的
module
作为它的参数,调用它不会引发TypeError
。
So in order to actually ask the questions again, you'll need a loop. 所以为了再次问问题,你需要一个循环。 For example:
例如:
while True:
name = raw_input('Enter your name: ')
age = raw_input('Enter your age: ')
qualifications = raw_input('Enter your Qualification(s): ')
print "Hello. Your name is {}. Your age is {}. Your qualifications are: {}".format(name, age, qualifications)
quit = raw_input("Is the above data correct [yY]? ").lower() # notice the lower()
if quit in ("y", "yes"):
break
else:
# If the input was anything but y, yes or any variation of those.
# for example no, foo, bar, asdf..
print "Rewrite the form below"
If you now input anything else than a y, Y
or any variation of yes
, the program will ask the questions again. 如果你现在输入任何比别的
y, Y
或任何变化yes
,该方案将再次提出的问题。
Move your A raw_input
line after you print the "Your name is..." and stuff. 在打印“您的名字是......”之后移动您的
A raw_input
行。 Like this: 像这样:
I also made the script keep asking to restart until the input is valid. 我还让脚本继续要求重新启动,直到输入有效。
Name = raw_input('Enter your name: ')
Age = raw_input('Enter you age:')
Qualifications = raw_input('Enter you Qualification(s):')
print "Hello" +'.'+ "Your Name is " + Name + ". " + "Your Age is " + Age + ". " + "Your Qualification(s) is/are:" + Qualifications + "."
print "If the above data is correct, type yes. If not, type no. Typing no will restart he program and make you write the form again."
yes = ['y', 'Y', 'yes', 'Yes', 'YES']
no = ['No' , 'no' , 'NO' ,'n' , 'N']
A = ''
while A not in (yes+no): # keep asking until the answer is a valid yes/no
A = raw_input("Again?: ")
if A in yes:
print 'Thanks for your submission'
if A in no:
reload()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.