我将如何实现以下内容:
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
我将如何实现以下内容:
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
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
(否定)。
这通常通过中间带有 a 的“循环半”结构来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."
此方法使您可以轻松打印一条消息,告诉用户程序期望什么。