【发布时间】: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