【问题标题】:How do I accept yes/no input from user and continue notifying them if they select no我如何接受来自用户的是/否输入并在他们选择否时继续通知他们
【发布时间】:2018-09-13 19:54:28
【问题描述】:

我希望接受用户的输入是/否,如果用户输入是,代码将继续运行可用的代码,如果用户输入否,代码将再次通知用户,代码不会运行,直到用户输入'是'终于

这里是简要介绍:

# Here is part1 of code


a = input('Have you finished operation?(Y/N)')

if a.lower()  == {'yes','y' }
    # user input 'y', run part2 code

if a.lower() == {'no', 'n'}
    # user input no
    # code should notifying user again "'Have you finished operation?(Y/N)'"  
    # part2 code will run until user finally input 'y'

if a.lower() !={'yes','y', 'no', 'n'}:
    # user input other words
    # code should notifying user again "'Have you finished operation?(Y/N)'" 


# here is part2 of code

您对如何解决此问题有任何想法,如果您能提供一些建议,我将不胜感激

更新

这是我正在尝试的代码

yes = {'yes','y',}
no = {'no','n'}

print(1)
print(2)
while True:
a = input('Have you create ''manually_selected_stopwords.txt'' ???(Y/N)''')
if a.lower().split() == yes:
    break
if a.lower().split() == no:
    continue

print(3)
print(4)  

运行时显示如下,我第一次尝试输入'n'然后'y',这总是会通知我,即使我输入'y'并且不会打印3和4

1
2
Have you create manually_selected_stopwords.txt ???(Y/N)>? n
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)>? y
Have you create manually_selected_stopwords.txt ???(Y/N)

【问题讨论】:

  • 人们应该真正了解 while 循环。
  • @cdarke 你错了,这就是你在 python 中编写集合的方式
  • @FlyingTeller:对,我忘了一套,但比较的是字符串。
  • @cdarke 那部分没有意义是的,OP的意思可能是a.lower() in {"yes", "y"}

标签: python python-3.x input pycharm interactive


【解决方案1】:

您的某些测试是不必要的。如果输入“no”与输入“marmalade”,结果相同。

# Here is part1 of code

while True:
    # Calling `.lower()` here so we don't keep calling it
    a = input('Have you finished operation?(Y/N)').lower()

    if a == 'yes' or a == 'y':
        # user input 'y', run part2 code
        break


# here is part2 of code
print('part 2')

编辑:

如果你想使用set 而不是or,那么你可以这样做:

if a in {'yes', 'y'}:

【讨论】:

    【解决方案2】:

    您正在比较 input()(字符串)的结果与字符串的 set 是否相等。他们永远不平等。

    您可以使用in 来检查您的输入是否在集合中:

    链接的欺骗asking-the-user-for-input-until-they-give-a-valid-response 应用于您的问题:

    # do part one
    yes = {"y","yes"}
    no = {"n","no"}
    
    print("Doing part 1")
    while True:
        try:
            done = input("Done yet? [yes/no]")
        except EOFError:
            print("Sorry, I didn't understand that.")
            continue
    
        if done.strip().lower() in yes:
            break # leave while
        elif done.strip().lower() in no:
            # do part one again
            print("Doing part 1")
            pass
        else:    
            # neither yes nor no, back to question
            print("Sorry, I didn't understand that.")
    
    print("Doing part 2")
    

    输出:

    Doing part 1
    Done yet? [yes/no]  # adsgfsa
    Sorry, I did not understand that.
    Done yet? [yes/no]  # sdgsdfg
    Sorry, I did not understand that.
    Done yet? [yes/no]  # 23452345
    Sorry, I did not understand that.
    Done yet? [yes/no]  # sdfgdsfg
    Sorry, I did not understand that.
    Done yet? [yes/no]  # no
    Doing part 1
    Done yet? [yes/no]  # yes
    Doing part 2
    

    【讨论】:

    • 非常感谢您的参考,我在尝试您的方法后更新我的问题,我仍然得到错误
    • @haochen 编辑了它 - 你正在将苹果(一个字符串)压缩到葡萄(一组字符串) - 这就是它不起作用的原因......
    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 2018-06-19
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多