Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
Poikkeusten Käsittely
Kun ohjelmoidaan, virheet tapahtuvat. Se on vain elämän tosiasia. Ehkä käyttäjä antoi virheellistä syötettä. Ehkä verkkoresurssi ei ollut saatavilla. Ehkä ohjelmalta loppui muisti. Tai ohjelmoija saattoi jopa tehdä virheen!
Pythonin ratkaisu virheisiin ovat poikkeukset. Olet saattanut nähdä poikkeuksen aiemmin.
print(a)
#virhe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Hups! Muuttujalle 'a' unohdettiin antaa arvo.
Mutta joskus et halua poikkeusten pysäyttävän ohjelmaa kokonaan. Saatat haluta tehdä jotain erityistä, kun poikkeus heitetään. Tämä tehdään try/except-lohkossa.
Tässä on yksinkertainen esimerkki: Oletetaan, että iteroit listan läpi. Sinun täytyy iteroida 20 lukua, mutta lista on käyttäjän antama syöte, eikä siinä välttämättä ole 20 lukua. Kun saavutat listan lopun, haluat vain, että loput luvut tulkitaan nollina. Näin voit tehdä sen:
def do_stuff_with_number(n):
print(n)
def catch_this():
the_list = (1, 2, 3, 4, 5)
for i in range(20):
try:
do_stuff_with_number(the_list[i])
except IndexError: # Poikkeus heitetään, kun yritetään päästä listan olemattomaan indeksiin
do_stuff_with_number(0)
catch_this()
Siinä se, ei ollut liian vaikeaa! Voit tehdä saman minkä tahansa poikkeuksen kanssa. Lisätietoja poikkeusten käsittelystä löytyy Python Docs
Exercise
Käsittele kaikki poikkeukset! Muista aiemmat oppitunnit palauttaaksesi näyttelijän sukunimen.
# Setup
actor = {"name": "John Cleese", "rank": "awesome"}
# Function to modify!!!
def get_last_name():
return actor["last_name"]
# Test code
get_last_name()
print("All exceptions caught! Good job!")
print("The actor's last name is %s" % get_last_name())
actor = {"name": "John Cleese", "rank": "awesome"}
def get_last_name():
return actor["name"].split()[1]
get_last_name()
print("All exceptions caught! Good job!")
print("The actor's last name is %s" % get_last_name())
test_output_contains("Cleese")
test_output_contains("All exceptions caught! Good job!")
test_output_contains("The actor's last name is Cleese")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!