使用单独的函数验证用户输入

2024-09-28 23:15:13 发布

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

我如何让它不返回“brea outside loop”。我已经阅读了关于stackoverflow的多个其他答案,但无法理解这个概念,因为它在一个单独的函数中。你知道吗

  def dayEntry(book,cmd):

          for x,type in zip(list, gettype):

                entry = input('?: ')
                validate(entry,type)
                results.append(entry)

        print (results)

def validate(inval, intyp):
    if intyp == "date":
        try:
            datetime.datetime.strptime(inval, '%d-%m-%Y')
        except ValueError:
            print("Format not valid, use DD-MM-YYYY")
            break

Tags: 答案loop概念datetimedeftypestackoverflowvalidate
2条回答

将validate函数设置为return某个指示有错误的内容,然后如果返回该内容,则中断循环。你知道吗

您必须在循环内执行break。你知道吗

很简单:您的break语句不在循环中。它在一个if语句中。你知道吗

相关问题 更多 >