New to Python and am trying to answer a homework problem where: a hospital records how many patients they are dealing with, the required nutrition to be provided to each patient, and then averaging required nutrition per patient after summing the totals.
Now when I'm testing/validating data entry, I see my code is causing errors because of the clumsy way I've tried to solve the problem. When testing, I get this:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
I've tried going through and messing with the return functions, but if it's not there, I think the issue might be with my read_input() functions. I've been messing around with PythonTutor, so I can visualize where the error is...I just have no idea how to get out of this loop and fix it.
my code so far
def validate_positive_patients(n):
try:
n = float(n)
if n <= 0:
print("Please enter a nonnegative number")
return n, False
except ValueError:
print("Please enter a positive integer")
return None, False
return n, True
def read_input(float):
value, positive = validate_positive_patients(input(float))
if not positive:
read_input(float=float)
else:
return value
# rest of code seems to work fine
My code is clumsy, but what I'd really like it to do is only accept int values for 'number of patients', floats for protein, carbs etc., and if there is an entry error initially to not just spit out a None value.
If only computers knew what you wanted them to do instead of what I'm telling it to do :P Thanks in advance for any help!
read_inputis wrongly indented.read_inputdoesn’t return anything if it recurses.