我一直在嘗試構(gòu)建一個函數(shù)來解釋命令行輸入,然后使用提供的參數(shù)從腳本文件中運行適當?shù)暮瘮?shù)。此函數(shù)中包含一個用于關(guān)閉交互層的轉(zhuǎn)義命令。僅當用戶輸入“退出”或“結(jié)束”時,才應(yīng)激活此轉(zhuǎn)義命令。但是,由于某種原因,無論輸入什么都會觸發(fā)。我難住了。你們有什么想法嗎?verinfo ()x = Truewhile 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"')
2 回答

MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
所以這里有兩個不同的概念,一個是身份,另一個是平等。
因此,關(guān)鍵字is
不是測試相等性,而是測試身份。意思是……這兩個對象是一樣的嗎?看看id()
。
我認為您可以查看很多試圖解釋 和 之間區(qū)別的==
頁面is
。
https://dbader.org/blog/difference-between-is-and-equals-in-python
http://www.blog.pythonlibrary.org/2017/02/28/python-101-equality-vs-identity/

GCT1015
TA貢獻1827條經(jīng)驗 獲得超4個贊
您應(yīng)該使用==
運算符進行此類比較,例如:
if procstring[0] == 'end' or procstring[0] == 'exit': print(...)
添加回答
舉報
0/150
提交
取消