patternpythonModerate
Determining whether a number is positive, negative or zero
Viewed 0 times
numberdeterminingpositivewhethernegativezero
Problem
I am a beginner in Python and am currently learning about making beautiful code. I've started with a small program. Can you please review my code?
def inputAndRes():
try:
a = raw_input("Enter no. :- ")
n = int(a)
except Exception:
print "-----------------------------------"
print "Please Enter No. " , "( The Number you entered is not a number )"
inputAndRes()
else:
if(n>0):
print "Number is Positive"
if(n<0):
print "Number is Negative"
if(n==0):
print "Number is ZERO"
wantRestart()
def checkYN(checkResult):
if (checkResult == 'Y'):
inputAndRes()
if (checkResult == 'N' ):
print (" !!! Program Quit !!!")
else:
print ("Invalid Option Make sure that you type (Y / N)")
print ("Continue..")
wantRestart()
def wantRestart():
print "\nWant to Restart Program, if Yes then Enter 'Y' and if No enter 'N' :- ",
try:
checkResult = raw_input(" ")
except Exception as e:
print ("Sorry Error Occured: ",e)
else:
checkYN(checkResult)
#...Start of Program...
inputAndRes()Solution
Flow control
The conditions here are mutually exclusive:
It would be better to rewrite with
Same thing here:
Should be:
Finally, instead of
Writing style
There is an official coding style guide for Python called PEP8. Give it a good read and try to follow it. Your most notable coding style violations:
Misc
Instead of running
The advantage of this is that you will be able to
Usability
The program wants me to enter
The conditions here are mutually exclusive:
if(n>0):
print "Number is Positive"
if(n<0):
print "Number is Negative"
if(n==0):
print "Number is ZERO"It would be better to rewrite with
elif and else:if n > 0:
print "Number is Positive"
elif n < 0:
print "Number is Negative"
else:
print "Number is ZERO"Same thing here:
if (checkResult == 'Y'):
inputAndRes()
if (checkResult == 'N' ):
print (" !!! Program Quit !!!")
else:
print ("Invalid Option Make sure that you type (Y / N)")
print ("Continue..")
wantRestart()Should be:
if checkResult == 'Y':
inputAndRes()
elif checkResult == 'N' :
print (" !!! Program Quit !!!")
else:
print ("Invalid Option Make sure that you type (Y / N)")
print ("Continue..")
wantRestart()Finally, instead of
raw_input(" "), it would be simpler to write as raw_input().Writing style
There is an official coding style guide for Python called PEP8. Give it a good read and try to follow it. Your most notable coding style violations:
- Spacing around operators: prefer
n > 0instead ofn>0
- Also, avoid unnecessary parenthesis in
ifstatements, for exampleif n > 0:is better thanif (n > 0):
- Function naming should be
snake_caseinstead ofcamelCase
- Put 2 empty lines in front of a function definition
Misc
Instead of running
inputAndRestart unconditionally, the common practice is this:if __name__ == '__main__':
inputAndRestart()The advantage of this is that you will be able to
import your module without running anything, because the if will only be true when you run the script directly.Usability
The program wants me to enter
Y to restart and N to exit, but I'm kinda lazy to press the shift. It would be great if I could use just y and n. Or even q and Q to exit. An easy way to do that:if result in 'yY':
input_and_restart()
elif result in 'nNqQ':
print (" !!! Program Quit !!!")Code Snippets
if(n>0):
print "Number is Positive"
if(n<0):
print "Number is Negative"
if(n==0):
print "Number is ZERO"if n > 0:
print "Number is Positive"
elif n < 0:
print "Number is Negative"
else:
print "Number is ZERO"if (checkResult == 'Y'):
inputAndRes()
if (checkResult == 'N' ):
print (" !!! Program Quit !!!")
else:
print ("Invalid Option Make sure that you type (Y / N)")
print ("Continue..")
wantRestart()if checkResult == 'Y':
inputAndRes()
elif checkResult == 'N' :
print (" !!! Program Quit !!!")
else:
print ("Invalid Option Make sure that you type (Y / N)")
print ("Continue..")
wantRestart()if __name__ == '__main__':
inputAndRestart()Context
StackExchange Code Review Q#62142, answer score: 16
Revisions (0)
No revisions yet.