'Try Except is just executing all lines simultaneously (in pyolite kernel with `input()`)

I'm "learning" Python in a course on Coursera, but this lesson doesn't have any explanation and doesn't seem to work at all. As I'm so new, I have no idea what is even going wrong in order to fix it or even glean how it should work from the lesson.

a = 1

try:
    b = int(input("Please enter a number to divide a"))
    a = a/b
except ZeroDivisionError:
    print("The number you provided cant divide 1 because it is 0")
except ValueError:
    print("You did not provide a number")
except:
    print("Something went wrong")
else:
    print("success a=",a)
finally:
    print("Processing Complete")

The lesson purports that this should output "success a=",a but instead it's just vomiting out Something went wrong / Processing Complete / Please enter a number to divide a [input box] and even when a number (let's say 2) is entered to the input box, it just terminates like Something went wrong / Processing Complete / Please enter a number to divide a 2 never actually printing the success. I've been over the lesson multiple times now and there's nothing to indicate why it's not working. Since Coursera has no way to contact instructors, I have no recourse except to outsource their job to here. (Sorry)

EDIT: To clarify, this is a course lab that launches a Jupyter notebook with the code already populated, and we execute the code to see how it works. It's running Pyolite kernel. It has nothing to do with me not copying it correctly as it is pre-populated in the notebook. I do not know why it is not working, but it's frustrating because I don't know what correct is supposed to look like, or if this IS correct, why it's not working in the notebook that I simply launch and execute.



Solution 1:[1]

It's not you. Your course shouldn't be using Jupyterlite with the pyolite kernel at this time. That is very cutting-edge to the point of being described as 'experimental' here. (Or, if the course administrators do so intentionally, they should make user's such as yourself very aware of the associated issues. As you are finding it is very frustrating & discouraging to be provided with something that is flawed when you are trying to learn.) It's well documented that input() doesn't work right there along with a host of other things, see here, here, here, here and links & comments therein. This will probably be a temporary situation as things are in very active development in the WASM/pyolite world.

As others have pointed out, you should provide more information from the get-go. You say it is a course that launches a pre-populated notebook. Provide the link so that we can use it without copying your code. Also with the URL in hand, I could probably tell you whether the people administering your course simply haven't perhaps noticed that the system now uses by default the pyolite kernel or whether they purposefully chose it. I have a feeling it is the former because there was an update to how the Try Jupyter page works. In other words, I suspect the course administrators probably didn't intentionally send you to some place using this kernel to run this code without providing guidance. The Try Jupyter page was switched to JupyterLite which uses the pyolite kernel, that actually runs all within the client's browser via web assembly, to try and reduce the traffic hitting the costly full server-backed system previously used there. However, as you found there's a few glitches. The change is relatively recent and the course instructors and moderators may simply not be aware of this. There's easy ways to offer other options. One is to send users to a site using a full Jupyter kernel backing the notebook. One such option illustrated:


Go to this GitHub page and click on launch binder badge you see there. A temporary Jupyter session will spin up.
Open a new notebook and paste in the code block you provided in your post. Run that cell.
You should see things work as expected.
Now in the future you can try things where they sent you and then when you encounter something that seems 'off' follow the same process to see if it is one of these glitches by spinning up another temporary session.


Unless something has dramatically changed since I took several Coursera courses, there are ways to contact course moderators/ administrators / mentors (who may or may not be able to directly contact the actual instructors). Those are who you would usually go to with something like this. There are usually course discussion boards. However, in this case, if what I suspect is happening is happening, the person or persons contacted would have ended up here or at the Jupyter Discourse forum inquiring as this is relatively new and has come up with other instructors not realizing what was occurring.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1