强迫一个原始的

2024-04-25 23:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我将如何实现以下目标:

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

Tags: andoftheinnumber目标inputyour
2条回答

这通常是用一个中间带有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."

此方法使您可以轻松地打印一条消息,告诉用户程序期望的内容。在

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

必须在hand之前将title_selection定义为'',这意味着空(也是False)。 not将使{}为真(否定)。在

相关问题 更多 >