3

我将如何实现以下内容:

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
4

2 回答 2

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(否定)。

于 2012-04-23T23:04:49.177 回答
11

这通常通过中间带有 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."

此方法使您可以轻松打印一条消息,告诉用户程序期望什么。

于 2012-04-23T23:07:57.897 回答