Skip to content
Advertisement

Tag: try-except

Why does my exit() command not work in python?

Below is my code to calculate calories based on input, after the print statements I want to exit the code and loop infinitely until the user inputs the right values. The code goes to the except even if the input values are right and does not exit the code, what am I missing? Answer exit() (some forms; it’s not actually

How to print every error using try except in python

I’m now learning how to handle multiple errors in python. While using try-except I want to print every error in try. There are two errors in try but the indexing error occurred first, so the program can’t print a message about ZeroDivisionError. How can I print both IndexErrormessage and ZeroDivisionErrormessage? Below is the code I wrote. Answer As the IndexError

How to make a variable inside a try/except block public?

How can I make a variable inside the try/except block public? This code returns an error How can I make the variable text available outside of the try/except block? Answer try statements do not create a new scope, but text won’t be set if the call to url lib.request.urlopen raises the exception. You probably want the print(text) line in an

Weird Try-Except-Else-Finally behavior with Return statements

This is some code that is behaving peculiarly. This is a simplified version of the behavior that I’ve written. This will still demonstrate the weird behavior and I had some specific questions on why this is occurring. I’m using Python 2.6.6 on Windows 7. Results: Why is demo one returning 3 instead of 1? Why is demo two printing 6

Advertisement