簡體   English   中英

如果命令,在停止程序退出Python之前該怎么做

[英]What to do from stopping the program from exiting in Python before if command

我使用以下代碼構建一個從用戶獲取輸入的簡單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()

程序在if命令之前完成。

除了['y', 'Y', 'yes', 'Yes', 'YES']['No' , 'no' , 'NO' ,'n' , 'N'] ,程序將完成並且不執行其各自if -clauses中的任何語句。

reload()函數不會按預期執行。 它用於重新加載模塊,應該在解釋器中使用。 它還需要先前導入的module作為它的參數,調用它不會引發TypeError

所以為了再次問問題,你需要一個循環。 例如:

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"

如果你現在輸入任何比別的y, Y或任何變化yes ,該方案將再次提出的問題。

在打印“您的名字是......”之后移動您的A raw_input行。 像這樣:

我還讓腳本繼續要求重新啟動,直到輸入有效。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM