if then elif

brad byte8bits at gmail.com
Wed Oct 10 22:58:32 CEST 2007

Shawn Minisall wrote:
> I just learned about if, then elif statements and wrote this program.  
> The problem is, it's displaying all of the possibilities even after you 
> enter a 0, or if the fat grams are more then the total number of 
> calories , that is supposed to stop the program instead of continuing on 
> with the print statements that don't apply.  Any idea's?  thanks

Two suggestions:

1. Use raw_input instead of input or errors will occur should users 
enter non-numeric characters.

>    #Prompt for calories
>    cal = input("Please enter the number of calories in your food: ")
>    #Prompt for fat
>    fat = input("Please enter the number of fat grams in your food: ")

2. Convert cal and fat to ints or floats for your calculations... maybe 
something like this:

except ValueError,e
    sys.exit(str(e) + "Enter a number!!!")

This will catch things that cannot be converted to an int (bad user input)

Besides that, I'm pretty sure your calculations are wrong.

More information about the Python-list mailing list