【发布时间】:2019-07-23 23:58:32
【问题描述】:
我希望用户通过准确地说“是”或“否”来决定是否删除选定的文件。无论出于何种原因,“是”有效,但“否”循环返回并决定返回初始 if 语句
我已经尝试了一些方法,经过一些谷歌搜索后,我似乎无法找到适合此问题的响应。我试过了:
使用 str() 和不使用 str() 更改 inputText
我尝试过在字符串文本上使用引号和撇号。
我已尝试更改初始 If 语句:
第一:
if inputText != ('yes' or 'no'):
我什至尝试过
第二:
if inputText != 'yes' or inputText != 'no':
第三:
if (inputText != 'yes') or (inputText != 'no'):
我得到的最接近的是 First。出于某种原因,“是”似乎有效,但“否”似乎循环回到第一个 If 语句。
谁能指出我正确的方向?我错过了什么?
inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
if inputText != ('yes' or 'no'):
inputText = input('Please type "yes" or "no" ')
elif inputText == 'yes':
##delete
print("pretend got deleted")
ctrl = False
elif inputText == 'no':
##shit does not get deleted
print("pretend doesn't got deleted")
ctrl = False
我预期的结果将是一个简单的
>> Are you sure you want to delete file? (yes/no) *random text that isn't 'yes' or 'no'*
>> Please type "yes" or "no"
这就是我在最初的 If 语句示例中得到的结果:
>> Are you sure you want to delete file? (yes/no) yes
>> pretend got deleted
这是我的预期:
>> Are you sure you want to delete file (yes/no) no
>> pretend doesn't got deleted
这是我实际得到的
>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no"
即使我这样做:
>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no" yes
>> pretend got deleted
是我得到的结果
【问题讨论】:
-
你可以使用
if (inputText != 'yes') and (inputText != 'no'): -
@Lyncius。看看我的回答,如果你不关注,请告诉我