I'm relatively new to Python. I just want some constructive feedback on how to improve my code efficiency, style, error handling, etc.
In this case, I've programmed the Ackermann function, but it won't evaluate well since it's built to handle the recursions relatively poorly - I'll have to fix that in the future. Any tips?
Also, if there are any areas of exploitation in the errors, please let me know too.
For reference, I'm programming this on Ubuntu 16.04 LTS and the language is Python 2.7.12
# Make sure the collected values are of the appropriate forms
def collection ():
while True:
# Collect raw_input for m_val and n_val
m_val = raw_input ("Please enter a nonnegative integer (m) : ")
n_val = raw_input ("Please enter another nonnegative integer (n): ")
# Make sure input is acceptable
try:
m_val = float(m_val)
n_val = float(n_val)
# m_val needs to be a nonnegative integer
# Check equivalence to 0 first
if m_val == 0:
pass
# Then check if the value is nonintegral
elif int(m_val) - m_val:
print("\nYou entered a float for m!\n")
continue
# Then make sure the value is nonnegative
elif m_val < 0:
print("\nYou entered a negative value for m!\n")
continue
# n_val needs to be a nonnegative integer
# Check equialence to 0 first
if n_val == 0:
pass
# Then check if the value is nonintegral
elif int (n_val) - n_val:
print("\nYou enterd a float for n!\n")
continue
# Then make sure the value is nonnegative
elif n_val < 0:
print("\nYou entered a negative value for n!\n")
continue
# If the code makes it this far, it should be ready for return
return int(m_val), int(n_val)
except ValueError:
print("\nPlease enter numerical values for m and n.\n")
def ackermann(m,n):
if m == 0:
return n + 1
elif m > 0 and n == 0:
return ackermann(m-1,1)
elif m > 0 and n > 0:
return ackermann (m-1, ackermann(m,n-1))
else:
print "The Value Doesn't Go into the Domain!"
(m,n) = collection()
print ( ackermann(m,n) )