命令行交互的字符串比较失败



我一直在尝试构建一个函数来解释命令行输入,然后使用提供的参数从脚本文件中运行适当的函数。此函数中包含关闭交互层的转义命令。仅当用户输入"exit"或"end"时,才应激活此转义命令。但是,由于某种原因,无论输入什么,它都会触发。我被难住了。你们中有人有想法吗?

verinfo ()
x = True
while x is True:
print ('Please seperate arguments with a comma.')
inputstring = input('->')
#format input string to a standard configuration
#expected is command,arg1,arg2,arg3,arg4,arg5
procstring = inputstring.split (',')
while len(procstring) < 6:
procstring.append('')
print (procstring)
#escape clause
print (procstring[0])
if procstring[0] is 'end' or 'exit':
print ('Closing')
x = False
break
elif procstring[0] is 'help' or 'Help':
Help ()
else:
print ('command invalid. List of commands can be accessed with "help"')

所以这里有两个不同的概念,一个是身份,另一个是平等。

因此,关键词is不是在测试平等,而是在测试身份。意义。。。这两个对象相同吗?看看id().

我想你可以看看很多页面,试图解释==is之间的区别。

  • https://www.geeksforgeeks.org/difference-operator-python/
  • https://dbader.org/blog/difference-between-is-and-equals-in-python
  • http://www.blog.pythonlibrary.org/2017/02/28/python-101-equality-vs-identity/

  • Python中None 的 identity 与平等

您应该使用==运算符进行此类比较,例如:

if procstring[0] == 'end' or procstring[0] == 'exit':
print(...)

最新更新