I am a self taught coder taking a Programming Fundamentals class to work towards a degree. It's based on Python, which I'm not as familiar with as other languages. I added error handling like I would in javascript or php to be an overachiever. Professor gave online feedback (online course) not to use try-except, that it is ahead of where we are in the semester, that even when we do get there, she doesn't want it in our code, and that this code is "not well formed" because I used try-except. She gave no feedback as to what she meant and I don't think that sounds right but don't now enough to stand my ground. Googling "well formed python" just give me a bunch of xml.
# main function
def main():
# @input dist : distance traveled in miles
# @input guse : gas used traveling dist
# @output mpg : dist/guse
# set dist from input "Enter miles traveled: "
dist = input('Enter distance traveled(in miles): ')
# make sure dist is a number, and if not print error and restart
try:
dist = float(dist)
except ValueError:
print('Distance traveled must be a number! You did not travel "' + \
dist + '" miles!')
main()
else:
# if dist is 0.0, print error and restart
try:
x = 1 / dist
except ZeroDivisionError:
print('Miles traveled cannot be 0!')
main()
else:
# set guse from input "Enter gas used in gallons: "
gcon = input('Enter gas consumed traveling(in gallons): ')
# make sure guse is a number, and if not print error and restart
try:
gcon = float(gcon)
except ValueError:
print('Gas consumed must be a number. You did not use "' + \
gcon + '"" gallons of gas!')
main()
else:
# set mpg to dist divided by guse
# if guse is 0.0, print error and restart
try:
mpg = dist / gcon
except ZeroDivisionError:
print('Gas consumed can not be 0!')
main()
else:
# return(print) mpg
print('Average miles per gallon(mpg):', format(mpg, '5.2f'))
# print request for enter keypress to end
input('press enter to continue')
# Call main function
main()
IF foo is not a number[newline][indent]PRINT error[newline][indent]RETURN[newline][no-indent]
. The first word of each line is similar to a BASIC keyword, for example IF, SET, PRINT, RETURN, FOR. – ChrisW Feb 1 '14 at 5:54