I asked more as a question of overall possibility rather than that specific example. I thought it would be an opportunity to smuggle data into/out of systems if things could be added to values generally accepted as known-goods. Guess I can’t really turn off the pen-test side of my brain.
Did I miss something about the try/except syntax, or are we just expected to look it up ourselves?
what is except
?
never heard of it
>>> a = 42
>>> a / 0
Traceback (most recent call last):
File "<pyshell#82>", line 1, in <module>
a / 0
ZeroDivisionError: division by zero
>>>
Above the traceback points to the line where a ZeroDivisionError exception was raised by the interpreter.
Because Python is script, and is not compiled until runtime, and then line by line, that means we can actually run it and let the interpreter raise an exception.
The language gives us tools to intercede when an exception is encountered, rather than allowing a fatal runtime error. Exceptions are fatal. There is no going back from one. We need to fix the code, and run it again. Or, we can predict in advance where an error could occur under the correct circumstances, such as the above division by zero, and redirect the error to a handler.
This topic falls under, Exception handling in the docs and in most courseware. Let 's consider an example:
>>> from random import randint
>>> while True:
... r = randint(0, 9)
... try:
... print (9 / r)
... except ZeroDivisionError:
... print ("Cannot divide by zero")
... break
...
...
2.25
1.0
1.125
3.0
Cannot divide by zero
>>>
It’s a simple enough example. We set up an infinite loop wherein we randomly generate an integer from zero to 9, both inclusive, and if the integer divided into 9 (the try
block) does not raise an exception, the quotient is printed. If it does raise an exception, this is where except
comes into play. The code that is contained in its block is called the ‘exception handler’. We print a message and, most importantly end the loop. But it only ends when an exception is raised, not before.
Error (Exception) handling is a concept common to script languages that are not pre-compiled. C and Java will not compile if there is an error in the code so we get the error message while we’re writing the code, not while it is running.
so except is
run [this code after the ‘except’ keyword] if an error is generated by the previous line?
That would be, if an error occurs in the try
block. We generally only wrap the exact line or lines that might raise an exception. All safe code would not be contained in the try block.
‘try’ block?
another one please! never can get enough of instructions the lessons don’t teach us!
The indented code that follows a signature line is the block.
if a and b:
# code block
while a:
# code block
for x in a:
# code block
def foo(x):
return x
try:
# try block
except:
# except block
does not compute, yet.
the two are used strictly together, yes?
can i convert a list into a string? if so, what does the string look like? can i just:
str(example_list)
or is the only option to manually iterate through the list somehow, that would be easier the other way around.
Suggest leave this until it comes up in the lessons. You are all over the map and don’t seem to be concentrating on your course work. One can hardly hope to get anywhere if you haven’t got your nose in the books.
this question wasn’t from this exercise, it was a question from a project i was working on but it doesn’t let me go to forums from a project, so i went back to a lesson i knew had access to ask my question, also got another question about compile
.
it shows up white in the editor as if it’s a built-in function
Sometimes people think they are ready for a project when in reality they are not, and usually it’s because of skipping over lessons or not doing a full review and follow up. Most of your questions can be answered with simple search.
can_we_count_it = [{‘s’: False}, “sassafrass”, 18, [“a”, “c”, “s”, “d”, “s”]]
for element in can_we_count_it:
if hasattr(can_we_count_it, “count”):
print(str(type(element)) + " has the count attribute!“)
else:
print(str(type(element)) + " does not have the count attribute :(”)
Codeacademy is not accepting my else statement (in Attribute Functions, q2). Any idea why this might be? My code does not produce any errors. But I get the response “Did you add an else statement?” and am not able to move forward in the exercise. Any idea what the issue might be? Thank you.
Hello,
it’s the same for me, I have yhe same error than you at the Instruction number 2
Any idea ?