Why test for success instead of testing for errors?


#1

Coding to my (very limited) experience is all about making the code as compact and clear as possible, while keeping it fully functional.
In the piylatin lessons we are end up putting the positive result in the very first ‘if’ statement, wouldn’t it be quicker to put it in the ‘else’ statement? This would allow us to pass through the errors first, maybe while giving some feedback to the console which errors did NOT trigger the code to stop functioning and once all possible sources of error would’ve been checked you would result in the actual output.

The only counter argument I could think of is this resulting in a program becoming slower (not having any experience I would like to ask how significant this slowing is)

I would love your input!
Have a nice day, Florian


#2

Down the road when we reach a production environment, yes, this is a concern. However we are only learning here and are not expected to improve or optimize exercise code. They are for practice and demostration. Suggest cool one’s jets and focus on following the instructions, rather than being overly critical of them.

Compare this to PE class in elementary school. Is it imperative that we purchase the newest in cutting edge trainers? No. Anything that fits and does not scuff the gymnasium floor will be more than adequate (and appropriate). We need to first learn how to tie laces so we don’t trip over them.

The following gives the correct (expected) results, but does not follow the instructions…

>>> original = input("Enter a word: ")
Enter a word: surprise
>>> if len(original) and original.isalpha():
	print (original[1:]+original[0]+'ay')
else:
	print ("empty")

	
urprisesay
>>> original = input("Enter a word: ")
Enter a word: 
>>> if len(original) and original.isalpha():
	print (original[1:] + original[0] + 'ay')
else:
	print ("empty")

	
empty
>>> 

#3

Your code will only execute one of those branches, not one and then the other, I don’t see how it would make any difference which branch you consider “successful”

And in any case, it wouldn’t matter which order you do something if you’re going to do them either way

What one could do though is to assume there’s no problem, not even check for it, and if there is, deal with it after the fact (eliminating error checking altogether in successful execution)

There’s nothing being done at scale here, so it’s likely not meaningful to talk about speed.


#4

I guess that indeed is a fair comparison, where for learning purposes this setup is more logical.

Furthermore, I have some small questions about the (very neat!) piece of code you wrote:
-am I correct to note that you did not put len(original) > 1, as any number greater than 0 would automatically result in a “True” comparison for And? (have done some further reading here and there and believe this might be the case due to the fact that a Boolean that != 0 automatically becomes true, is this correct and is this indeed the reason that this code works?)
-in the successful print statement you use 1: instead of 1:len(original) as suggested by the lesson. Do the parentheses automatically complete to the end of the word if no input is given?

Also, here and there I have seen the >>> quite a bit, what are they all about? Is is there to indicate the start of a new block of code? When I tried to use it in the lessons themselves it returned an error so idk…

Thank you a lot for the help you’ve provided already, you’re very helpful!


#5

A non-zero number will evaluate to truthy in a conditional or logical expression. In this case, length is always positive (if not zero), but the same would be true for negatives, as well. the key word here is non-zero.

That is the command prompt of the interactive shell for IDLE, Python’s built in IDE. Those lines have been typed directly into the command line so give an immediate response. It is not part of the code.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.