QUESTION IMAGE
Question
arrasira y suelta las palabras clave para crear un programa que imprima unavailable en la consola
prices = { \pizza\ : 3.00 }
try:
charge = prices\calzone\
print(\charged\)
print(\unavailable\)
print(\out of range\)
To solve this, we analyze the Python code structure for error handling (try - except block):
Step 1: Understand the Context
We have a dictionary prices with only the key "pizza". When we try to access prices["calzone"], a KeyError will occur (since the key doesn't exist). We need to catch this error and print "Unavailable".
Step 2: Structure of try - except in Python
In Python, the try block contains code that might raise an error. The except block catches the error (we can specify the type of error, here KeyError). The general structure is:
try:
# code that might raise error
except ErrorType:
# code to handle the error
In our case, after the try block (which has the code that raises KeyError), we need to add an except block to catch KeyError and then print "Unavailable".
Step 3: Filling the Blanks
The correct code after the try block (after print("Charged") which will not execute because the error occurs before it) should be:
except KeyError:
print("Unavailable")
So the key - words to drag and drop are:
- First,
except KeyError:(to catch theKeyErrorwhen accessing a non - existent key in the dictionary) - Then, the code
print("Unavailable")is already there, and we don't need theprint("Out of range")as it's not related to aKeyErrorin this context. Also, theelseblock is not needed here because we expect an error to occur.
So the correct sequence in the blank (after print("Charged") and before print("Unavailable") and print("Out of range")) is to place except KeyError: and then the print("Unavailable") is indented under it. But in terms of the drag - and - drop elements:
- The first element to place in the blank (the line after
print("Charged")) isexcept KeyError: - Then, the
print("Unavailable")is already there, and we can ignore theelseand theprint("Out of range")(or removeprint("Out of range")as it's incorrect here).
If we consider the drag - and - drop options:
- The first blank (the line after
print("Charged")) should be filled withexcept KeyError: - Then, the
print("Unavailable")is as is, and we don't useelsehere.
So the answer is to place except KeyError: in the blank line (the line between print("Charged") and print("Unavailable")), and then the print("Unavailable") is under it (indented). But in the given options, the key - words to use are except KeyError: and then the print("Unavailable") is already there. So the main part to drag and drop is except KeyError: into the blank line (the line after print("Charged")).
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
To solve this, we analyze the Python code structure for error handling (try - except block):
Step 1: Understand the Context
We have a dictionary prices with only the key "pizza". When we try to access prices["calzone"], a KeyError will occur (since the key doesn't exist). We need to catch this error and print "Unavailable".
Step 2: Structure of try - except in Python
In Python, the try block contains code that might raise an error. The except block catches the error (we can specify the type of error, here KeyError). The general structure is:
try:
# code that might raise error
except ErrorType:
# code to handle the error
In our case, after the try block (which has the code that raises KeyError), we need to add an except block to catch KeyError and then print "Unavailable".
Step 3: Filling the Blanks
The correct code after the try block (after print("Charged") which will not execute because the error occurs before it) should be:
except KeyError:
print("Unavailable")
So the key - words to drag and drop are:
- First,
except KeyError:(to catch theKeyErrorwhen accessing a non - existent key in the dictionary) - Then, the code
print("Unavailable")is already there, and we don't need theprint("Out of range")as it's not related to aKeyErrorin this context. Also, theelseblock is not needed here because we expect an error to occur.
So the correct sequence in the blank (after print("Charged") and before print("Unavailable") and print("Out of range")) is to place except KeyError: and then the print("Unavailable") is indented under it. But in terms of the drag - and - drop elements:
- The first element to place in the blank (the line after
print("Charged")) isexcept KeyError: - Then, the
print("Unavailable")is already there, and we can ignore theelseand theprint("Out of range")(or removeprint("Out of range")as it's incorrect here).
If we consider the drag - and - drop options:
- The first blank (the line after
print("Charged")) should be filled withexcept KeyError: - Then, the
print("Unavailable")is as is, and we don't useelsehere.
So the answer is to place except KeyError: in the blank line (the line between print("Charged") and print("Unavailable")), and then the print("Unavailable") is under it (indented). But in the given options, the key - words to use are except KeyError: and then the print("Unavailable") is already there. So the main part to drag and drop is except KeyError: into the blank line (the line after print("Charged")).