An warware: string%3A index kuskure

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.

Shafi posts:

Leave a Comment