As per janos flow control and PEP8 are important. I agree with what's been said; however there's more to the flow of the code than has been stated so far. Also a few additional points to bear in mind
This is potentially bad practice; it depends on what your code is going to be used for. I would never catch all exceptions within the main part of your code itself. If your code is generating exceptions which you're not expecting then you really need to know about it. Equally, if a user wants to stop the code from running through a
KeyboardInterrurpt you've just forbidden this.
Catch only the exceptions that you're expecting to get and that you have a strategy for dealing with. If it's important that your code doesn't error then wrap in a highly generic exception handler only where necessary.
From Python 3
print is a function; you're obviously using Python 2.x by your use of the print statement:
print "Number is Positive"
As your version of Python supports
print() use it and get into the good habits now.
- Sometimes you print something before using
raw_input() without an argument; sometimes you use
raw_input() with an argument.
- Both the statement
print and the function
print() are used.
It's important to be consistent as it makes your intent clearer and the code cleaner. All useful when you re-look at code in a years time or a colleague does so after you've left. You want
Though it's fairly easy to understand what's happening get into the habit of writing comments.
Use docstrings at the top of every method to explain what that method does and how you use it. Use comments to explain why your code is doing what it does and in the case of complex code what as well. To give an example consider the following two situations
# increment i by 1
i += 1
# Want to count the number of iterations
# for use in method check() later
i += 1
The first comment explains what's happening, which is obvious from the code. The second explains why it's happening, which means that people might find the code easier to understand.
If you were to create a decision tree of your code then it would be a little confused;
inputAndRes() calls itself and
Careful usage of the
break statement which exits a loop and the
continue statement which "continues to the next cycle of the nearest enclosing loop" will make your code simpler and remove the need for recursion.
if statement to determine if the user wants to continue is quite verbose. You can use Python's ability to return a Boolean based on a test to see whether they want to continue using your program. From a usability perspective I'd only test for a positive. If they want to exit then let someone type anything.
return checkResult in 'yY'
This will return True if the user input
y and False otherwise.
Very minor, but there's no need for
a = raw_input("Enter no. :- ")
n = int(a)
You're creating two objects and the statements are simple. You can attempt the conversion to an
a = int(raw_input())
Putting all of this together and using the other comments about
__main__ and Pep8 etc. I think I'd end up with this:
"""Determine if the user wants to continue playing."""
check = raw_input("Press 'Y' to restart: ")
# Only care if people want to continue.
return check in 'Yy'
"""Accepts a number from the user and decides whether
It's positive, negative or 0."""
n = int(raw_input("Enter number: "))
if n > 0:
print("Number is Positive")
elif n < 0:
print("Number is Negative")
print("Number is Zero")
if not want_restart():
print (" !!! Program Quit !!!")
# If the number is not a number we can force
# a restart of the process.
print ("The Number you entered is not a number; please retry")
if __name__ == "__main__":