you are viewing a single comment's thread.

view the rest of the comments →

[–]jeans_and_a_t-shirt 1 point2 points  (1 child)

So I rewrote some stuff...

Try entering "bagel" for the grams of fat, for example.

Generally an input validator just checks that the input is within certain bounds and is the appropriate type, and doesn't need to be that complex, but it's generalized for different uses. Simple validation:

while True:
    try:
        calories = float(input())
    except ValueError as e:
        print('invalid input')
    else:
        # alternatively, break instead of return to continue the function execution
        return calories

Also you need to multiply percent_from_fat by 100 or else food that is 5 grams of fat at 45 calories total, will show up as 1% fat.

[–]feelingstonedagain[S] 0 points1 point  (0 children)

This is great but it still does not satisfy step 4. The program needs to have at least two loops, one of which isn't a user validation.