在Python中,切片結束值爲獨佔。你是切片比你認爲你是一個小清單:
>>> begin=['physical','Physical','Software','software',]
>>> begin[2:3]
['Software']
>>> begin[0:1]
['physical']
使用begin[2:4]
和begin[0:2]
甚至begin[2:]
和begin[:2]
獲得從3日到結尾的所有元素,並從一開始,直到2日(含):
>>> begin[2:]
['Software', 'software']
>>> begin[2:4]
['Software', 'software']
>>> begin[:2]
['physical', 'Physical']
>>> begin[0:2]
['physical', 'Physical']
更好
然而,使用str.lower()
來限制你需要提供的輸入數量:
if answer.lower() == 'software':
只有一個字符串需要測試,現在可以將你的函數放入字典中;這可讓您選擇列出各種有效回答過:
options = {'software': software, 'physical': physical}
while True:
answer = input('Please enter one of the following options: {}\n'.format(
', '.join(options))
answer = answer.lower()
if answer in options:
options[answer]()
break
else:
print("Sorry, {} is not a valid option, try again".format(answer))
我看到很多洞,你的輸入是什麼?提供樣本輸入和預期輸出。看看什麼是[mcve]。 –
當我嘗試輸入軟件/軟件 –
也輸入分配給答案 –