Question

In: Computer Science

Write a Python program that uses function(s) for the following problem: A nutritionist who works for...

Write a Python program that uses function(s) for the following problem:

A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:

\(calories\:from\:fat\:=\:fat\:grams\:\times9\)

Next, she calculates the number of calories that result from the carbohydrates, using the following formula:

caloriesfromcabs=carbgrams×4

The nutritionist asks you to write a program that will make these calculations. Make sure that your program does not allow user to enter negative values for fat grams and carb grams (that is, validate the input with a loop).

Note:

you can write one function that takes as its parameters the number of grams and calories generated per gram respectively, and calculates and returns the total calories generated. You can call the function twice, once for fat grams and once for carb grams. For example,

calculate_calories(30.5 , 9) will return 274.5

calculate_calories(75.8, 4) will return 303.2

Solutions

Expert Solution

#program
#calories from fat= fat_Grams * calories per gram of fat
#calories from carbohydrates=carb_grams* 4
def calculate_calories(taken_grams,cal_per_gram):
    #checking whether user given -ve info for taken grams parameter
    if taken_grams<0:
        print("enter non negative value for taken_grams")
        #if error occurs it will return None
        return None
    #by using the above formula
    total_calories=taken_grams*cal_per_gram
    #return the totl calories
    return total_calories

#calling the function for fats
fat_calories=calculate_calories(30.5,9)
print("fat_calories:= ",fat_calories)
#calling the function for carbohydrates as cal per gram for carbohydrates is 4
carb_calories=calculate_calories(75.8,4)
print("carb_calories:= ",carb_calories)
#checking input with negative value for taken_grams
calculate_calories(-3,9)

#output

fat_calories:= 274.5
carb_calories:= 303.2
enter non negative value for taken_grams

#code snippet


#calories from fat= fat_Grams * calories per gram of fat
#calories from carbohydrates=carb_grams* 4 
def calculate_calories(taken_grams,cal_per_gram):
    #checking whether user given -ve info for taken grams parameter
    if taken_grams<0:
        print("enter non negative value for taken_grams")
        #if error occurs it will return None
        return None
    #by using the above formula
    total_calories=taken_grams*cal_per_gram
    #return the totl calories
    return total_calories

#calling the function for fats
fat_calories=calculate_calories(30.5,9)
print("fat_calories:= ",fat_calories)
#calling the function for carbohydrates as cal per gram for carbohydrates is 4
carb_calories=calculate_calories(75.8,4)
print("carb_calories:= ",carb_calories)
#checking input with negative value for taken_grams
calculate_calories(-3,9)

#screenshots of code

#screenshot of output


Related Solutions

Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
Who knows to do this topic with python code? Write a program to perform the following...
Who knows to do this topic with python code? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and...
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder,...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder, circle, cone and sphere. For each of this shape, write a separate function. Let your function take in 2 inputs only. Your program should prompt user to enter these 2 inputs and print out the results of the volumes of the shape separately?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT