Been struggling for hours with this little thing: Exceptions

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Been struggling for hours with this little thing: Exceptions

Postby pythonnoob2 » Fri Sep 16, 2016 3:56 pm

I've completely new to programming and need a little help with Error handling...

I have to solve a task that reads:

Write a procedure def trialAndError(), which calls an already defined function process(). The function should

- output 'Succeeded' if no error was raised in the process() function.
- output 'TypeError' caught if the process() function raised a TypeError
- output 'ValueError' caught if the process() function raised a ValueError
- output 'ZeroDivisionError' caught if the process() function raised a ZeroDivisionError

This exercise is on some kind of Python program aimed to help students learn Python, so I have to run the code in this program.

This is my code: link removed code added below ,
Code: Select all
def process():
    try:
        print "Succeeded"
    except TypeError:
        print "TypeError caught"
    except ValueError:
        print "ValueError caught"
    except ZeroDivisionError:
        print "ZeroDivisionError caught"
 
def trialAndError():
    process()
 
trialAndError()
            # your code goes here

but it's not quite right and I have no idea what I'm doing wrong. When I'm running it in pyCharm, it works fine and prints Succeeded. However, when I run it in the learning program I get all sorts of errors. I think that the program is randomizing errors when the code is executed, and if my code would work it would print any of the exceptions, but it doesn't. I think that's also why it works in pyCharm, because there are no random errors there. Bad explanation but I hope you understood... Any help would be appreciated!!
Last edited by Yoriz on Fri Sep 16, 2016 7:03 pm, edited 1 time in total.
Reason: First post lock. Added code from link to code tags. Added exceptions to subject.
pythonnoob2
 
Posts: 4
Joined: Fri Sep 16, 2016 3:49 pm

Re: Been struggling for hours with this little thing: Except

Postby Yoriz » Fri Sep 16, 2016 7:08 pm

As far as I can make out, it seems to me you have got a little mixed up.
You have created a process function or maybe you have altered the given process function.
It reads that the trialAndError function is where the code has to be that should output the applicable string for each error or no error it will just call the given process function.

When you run it in the learning program I expect the process function is changing to give the various errors that your function is expected to react to.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Been struggling for hours with this little thing: Except

Postby pythonnoob2 » Fri Sep 16, 2016 8:11 pm

Yoriz wrote:As far as I can make out, it seems to me you have got a little mixed up.
You have created a process function or maybe you have altered the given process function.
It reads that the trialAndError function is where the code has to be that should output the applicable string for each error or no error it will just call the given process function.

When you run it in the learning program I expect the process function is changing to give the various errors that your function is expected to react to.


Ohh so it should be more like this?
Code: Select all
def trialAndError():
   
    try:
        print "Succeeded"
   
    except TypeError:
        print "TypeError caught"
   
    except ValueError:
        print "ValueError caught"
   
    except ZeroDivisionError:
        print "ZeroDivisionError caught"
   
trialAndError()


I just don't know how to call the process() function, because I always get an error message that says 'process is not defined' or something like that
pythonnoob2
 
Posts: 4
Joined: Fri Sep 16, 2016 3:49 pm

Re: Been struggling for hours with this little thing: Except

Postby Yoriz » Fri Sep 16, 2016 8:30 pm

The call to process should be inside the try block before the print "Succeeded" otherwise the try block wont capture process errors and the try block will always be successful.
if you were not given a process function just create one that does nothing
Code: Select all
def process():
    pass
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Been struggling for hours with this little thing: Except

Postby pythonnoob2 » Fri Sep 16, 2016 8:46 pm

Yoriz wrote:The call to process should be inside the try block before the print "Succeeded" otherwise the try block wont capture process errors and the try block will always be successful.
if you were not given a process function just create one that does nothing
Code: Select all
def process():
    pass

Alright, almost got it now with this:
Code: Select all
def process():
    pass
def trialAndError():
    try:
        process()
        print "Succeeded"
   
    except TypeError:
        print "TypeError caught"
   
    except ValueError:
        print "ValueError caught"
   
    except ZeroDivisionError:
        print "ZeroDivisionError caught"
trialAndError()


The only thing now is that I got one extra line on the output that says 'succeeded' that shouldn't be there. The correct answer is 8 randomized lines and I got them all, but I got 9 lines cause of that extra 'succeeded' string. Any idea what might cause it?
pythonnoob2
 
Posts: 4
Joined: Fri Sep 16, 2016 3:49 pm

Re: Been struggling for hours with this little thing: Except

Postby Yoriz » Fri Sep 16, 2016 8:52 pm

It could be your own call to
Code: Select all
trialAndError()
At the end of your code that is not supposed to be there.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Been struggling for hours with this little thing: Except

Postby pythonnoob2 » Fri Sep 16, 2016 8:55 pm

Yoriz wrote:It could be your own call to
Code: Select all
trialAndError()
At the end of your code that is not supposed to be there.

Yeah, that was it! Thanks a lot for the help man, really needed it!
pythonnoob2
 
Posts: 4
Joined: Fri Sep 16, 2016 3:49 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 1 guest

cron