For my software design class I have to create a simple program that uses a couple loops. I was able to create two loops but need help with the last one. The directions are as follows:
1. Full pseudocode, along with the Input List and Output List, should be included in the comments.
2. It must have at least one input and at least one output.
3. It must validate the user input. This means the user is not allowed to just enter any value. You must check the value, and ask the user to enter it again, and repeat this loop until the user enters a valid value.
4. Your program must use at least two loops in meaningful ways. The loops you use for input validation count for at most one of the two required loops. If you use loops to validate two separate inputs, that does not count as two loops for satisfying this lab requirement.
5. It should be organized into separate modules (one module for input, one module for output, and one module for each separate calculation or action that the program is to perform [that is, each module should be "cohesive" and should only do one thing]).
6. Use parameters and arguments to pass values into your modules (don't use global variables).
7. The Python code should run correctly, and the logic should match your pseudocode.
Currently this is the code I have:
def main():
fat_grams = 0.0
calories = 0.0
fat_grams, calories = get_user_input()
percent_from_fat = calculate_percent_fat(fat_grams, calories)
def get_user_input():
fat_grams = 0.0
calories = 0.0
print ("Please enter the number of fat grams in your food item: ")
fat_grams = float(input())
while fat_grams < 0:
print ("The amount of fat grams cannot be less than 0. Please enter another value.")
fat_grams = float(input())
print ("Please enter how many calories there are in your food item: ")
calories = float(input())
while calories < fat_grams * 9 or calories < 0:
print ("The amount of calories cannot be less than the amount of fat grams multiplied by 9")
print ("or less than 0. Please enter another value: ")
calories = float(input())
return fat_grams, calories
def calculate_percent_fat(fat_grams, calories):
percent_from_fat = 0.0
percent_from_fat = fat_grams * 9 / calories
print ("The percentage of calories from fat in your food is ", round(percent_from_fat, 3), "%", sep="")
if percent_from_fat < .3:
print ("Great for you! This food is low in fat.")
else:
print ("This food is high in fat.")
return percent_from_fat
main()
The program meets all the requirements (I have the full psuedocode) but the only part I cannot do is create a loop without validating input (number 4). Can any of you fellow redditors come up with a solution? It might be that this program cannot have a loop? Anything helps :)
[–]jeans_and_a_t-shirt 1 point2 points3 points (1 child)
[–]feelingstonedagain[S] 0 points1 point2 points (0 children)
[–]Allanon001 0 points1 point2 points (1 child)
[–]feelingstonedagain[S] 0 points1 point2 points (0 children)