A matsayin mai haษakawa, abu ne na gama-gari don saduwa da Kuskuren Fihirisar Kirtani. Wannan yana faruwa ne lokacin da kake ฦoฦarin samun dama ga fihirisar da ke waje da kewayon madaidaicin halal a cikin kirtani ko sanya a sauฦaฦe, lokacin da fihirisar da kake ฦoฦarin ษauko ba ta wanzu a cikin wannan kirtani.
Tushen wannan kuskuren ya ta'allaka ne akan yadda Python ke fassara fihirisar kirtani. A Python, fihirisar kirtani tana farawa daga 0 kuma ta haura zuwa tsayin kirtani a debe 1. Don haka, maฦasudin halal na ฦarshe a cikin kirtani zai zama Tsawon Kirta - 1. Idan ka wuce wannan kewayon, Python zai jefa Kuskuren Index. tare da ฦarin saฦon 'kirtani daga kewayon'.
Maganin don wannan matsalar mai sauฦi ce - kuna buฦatar tabbatar da cewa kuna samun dama ga ingantattun fihirisa a cikin kewayon.
Lambar Python don Magance Kuskuren Fihirisar Kirtani
def check_index(string, index): try: return string[index] except IndexError: print('String index out of range!') return None # Checking the function print(check_index('Python', 5)) # 'n' print(check_index('Python', 6)) # String index out of range!
A cikin lambar da ke sama, muna ayyana aikin check_index wanda ke ษaukar kirtani da fihirisa azaman sigogi. Wannan aikin yana ฦoฦari ya dawo da hali a fihirisar da aka bayar. Idan fihirisar da aka bayar ba ta cikin kewayo, toshe kama zai kama keษanta, ya nuna saฦon da ya dace, kuma ya dawo Babu.
Bayanin Code
Lambar tana farawa ta hanyar ayyana wani aiki da ake kira check_index, wanda ke ษaukar sigogi biyu: kirtani da fihirisa. A mataki na farko, ta amfani da toshe gwadawa, aikin yana ฦoฦarin mayar da hali a matsayin da aka ฦayyade ta fihirisa.
- Idan fihirisar ta wanzu a cikin kewayon kirtani, Python ya sami nasarar dawo da halin.
- Idan fihirisar ta fita daga kewayon kirtani, Python yana jefa IndexError.
Don magance kuskuren, muna amfani da katangar kama wanda ke ษaukar banbanin IndexError. Lokacin da aka kama keษaษษen, ana buga saฦo yana sanar da mai amfani cewa fihirisar kirtani ba ta da iyaka. Bayan haka, aikin ya ฦare kuma ya dawo Babu.
Dakunan karatu na Python masu alaฦa da Ayyuka
Yin aiki tare da kirtani a cikin Python an sauฦaฦe shi tare da tarin ayyukan ginanni da ษakunan karatu. The rance () aiki yawanci ana amfani dashi don gano tsawon kirtani. Wannan na iya taimakawa wajen tabbatar da cewa fihirisar da kuke ฦoฦarin shiga ba ta wuce tsayin kirtani ba. Python's str ษakin karatu yana da amfani don aiki tare da sarrafa igiyoyi.
A cikin mahallin sarrafa kurakurai, an gina Python a ciki gwada/sai dai tubalan suna ba da damar ingantaccen sarrafa keษantawa, yana sa lambar ku ta fi ฦarfi da aminci.
Fahimta da yadda ya kamata mu'amala da keษantacce kamar Kurakurai na Index na String ba kawai zai haษaka ฦwarewar shirye-shiryenku ba amma kuma yana taimakawa wajen haษaka ฦaฦฦarfan ฦaฦฦarfan aikace-aikacen Python marasa kuskure. Tare da ingantattun hanyoyin magance kuskuren Python, zaku iya hangowa, kamawa, da kuma sarrafa waษannan keษancewar, haษaka ingancin software gaba ษaya.