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))

空(Falseでもある)を意味する事前に定義する必要がありますtitle_selection''はTrue (否定) になりますnotFalse

于 2012-04-23T23:04:49.177 に答える
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."

このメソッドを使用すると、プログラムが何を期待しているかをユーザーに伝えるメッセージを簡単に出力できます。

于 2012-04-23T23:07:57.897 に答える