【问题标题】:I am trying to make a yes or no question that if you click no it askes the question again [duplicate]我正在尝试提出是或否的问题,如果您单击否,它会再次提出问题[重复]
【发布时间】:2021-09-11 23:16:26
【问题描述】:

我是 python 新手,我想做一些问你的事情:是或否。如果用户说是,它会做某事,如果他们说不,它会再次询问问题,直到他们说是。我该怎么做呢?提前致谢!

【问题讨论】:

  • 您能展示一下到目前为止您尝试过的内容吗?

标签: python python-3.x python-2.7 while-loop


【解决方案1】:

一个简单的例子是:

answer = input('Your question. Please answer yes or no.')

while answer != 'yes':
    answer = input('Your question. Please answer yes/no.')

# executes only once answer == 'yes'
something()

【讨论】:

    【解决方案2】:

    带有一些断言的简单 while 循环...

    answer = input('Do you agree? y/n')
    assert answer in ['y', 'n'], 'Please answer only with a "y" or a "n"'
    answer = input('Do you agree? y/n')
    while answer == 'n':
        answer = input('Do you agree? y/n')
        assert answer in ['y', 'n'], 'Please answer only with a "y" or a "n"'
    # If we're here the user has finally answered "y"
    do_something()
    
    
    

    【讨论】:

      【解决方案3】:
      while True:
          answer = input('Question: ')
          if answer == 'Yes':
              # do something
              break
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 2015-01-23
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        相关资源
        最近更新 更多