我正在創建一個代碼,當用戶在對象列表中選擇超出範圍的輸入數字時,我想創建一條錯誤消息。我正在使用的代碼如下:當輸入超出列表中的範圍時執行錯誤消息 - python
choose = int(input('Which one would you like to do a fundamental analysis on?:'))
share = (object_list[choose - 1])
print('\n-----Fundamental analysis for ' + share.company_name + '-----')
print('The company solidity is:')
print(share.solidity)
print('The company p/e value is:')
print(share.p_e)
print('The company p/s value is:')
print(share.p_s)
預先感謝您!
使用'try' /'除了IndexError:' – JohanL
@coldspeed:很好找。但是,原始答案不檢查負指數。嘗試/除非在該特定情況下效率低下。那麼,我會回答這樣的原始問題,但我回答了一個,而不是... –