如何接受来自用户的 yes/no 输入并在他们 select 否时继续通知他们

How do I accept yes/no input from user and continue notifying them if they select no

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

简单介绍如下:

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

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

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

Linked dupe 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

您的某些测试是不必要的。如果键入 "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'}: