0

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
Vort
  • 11
  • 1
  • 1
    I ran your code in a Jupyter notebook, and could reproduce the expected behaviors: successful division, division by 0 error, not a number error. I did not see the behavior you are describing. What platform are you using in order to enter and execute your code? – Amitai Irron Apr 21 '22 at 23:39
  • 1
    I think you should provide exact input and expected by you output in more clear format. Like Input: 0, I would expect that message, the program gives me X, why is that, and so on. Then I can explain your doubts. – Piotr Grzybowski Apr 21 '22 at 23:39
  • Maybe you just copied over the code from the website and something is not quite right with the indentation/tabulators (space vs. tabs)? That would explain why it does not work as you would expect. – Kristof Rado Apr 21 '22 at 23:43
  • It's a Jupyter notebook through the Coursera lab running Pyolite kernel if that helps. When entering an integer (2 in the example I gave), it outputs just like I mentioned. A different line for each: Something went wrong n/ Processing Complete n/ Please enter a number to divide a [input box]. Enter 2 in the [input box] and no lines change, [input box] disappears and just locks in whatever integer I entered, so at last it looks like: Something went wrong n/ Processing Complete n/ Please enter a number to divide a 2 – Vort Apr 21 '22 at 23:58
  • I have opened a new workbook and copy-pasted the code; it is still not working. – Vort Apr 22 '22 at 01:19
  • If you comment out the `except:` and its print statement, you will be able to exactly which exception you are getting. Perhaps that will help identify the problem. – Tim Roberts Apr 22 '22 at 04:07
  • I commented stuff out and those lines didn't run, but even the fundamental first part of the code isn't working. It produces the input box but never runs the a / b no matter what I put in, be it integer text etc. – Vort Apr 22 '22 at 06:18

1 Answers1

0

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Holy hell, thank you! I had moved on but I was extremely frustrated that it wasn't working, and beyond that, that I couldn't figure out WHY it wasn't working. Even more frustrating was that I couldn't find documentation of anyone else was having the issue (more likely they just shrugged and moved on). Regarding providing more info, I'll also chalk that up to my inexperience, as I didn't know what I didn't know. Anywho, jesus that's a relief. Thank you. – Vort Apr 23 '22 at 03:41
  • I'd upvote but I only have 11 reputation, and upvoting requires 15. – Vort Apr 23 '22 at 03:57