2012-04-23 23 views
3

我將如何執行以下操作:強制的raw_input

title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string)) 
if not title: 
    # repeat raw_input 

回答

11

這往往與「環式和半」在中間的break構建完成:

while True: 
    title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string)) 
    if title_selection: 
     break 
    print "Sorry, you have to enter something." 

這種方法給你一個簡單的機會打印一個信息告訴用戶什麼計劃期望。

12
title_selection = '' 
while not title_selection: 
    title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string)) 

有必要定義title_selection''前手,這意味着空(也爲False)。 not將使False爲真(否定)。