【问题标题】:Input validation of two conditions [duplicate]两个条件的输入验证[重复]
【发布时间】:2018-04-20 00:40:38
【问题描述】:

我正在验证 python 中的输入。 这是我到目前为止的代码:

while input('Would you like to continue(y=yes/n=no)') not in ('y'): 
      print ("Sorry, I didnt catch that. Enter again: ")

代码有效,但我想使用两个条件,一个用于“y”,一个用于“n”。

【问题讨论】:

  • 我上面链接的那个问题的最佳答案显示了如何存储输入的内容,然后你可以检查它以做任何你想做的事情。

标签: python validation


【解决方案1】:

试试这个:

while True:

    ## Get their choicer
    selection = input('Your message')

    ## If yes
    if selection == 'y':
        do_something_for_y()

    ## If no
    elif selection == 'n':
        do_something_for_n()

    ## If neither, this loops to the top and asks again

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多