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...
FOR PYTHON: Write a python program for a car salesperson who works a five day week....
FOR PYTHON: Write a python program for a car salesperson who works a five day week. The program should prompt for how many cars were sold on each day and then prompt for the selling price of each car (if any) on that day. After the data for all five days have been entered, the program should report the total number of cars sold and the total sales for the period. See example output. NOTE: duplicate the currency format shown...
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...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
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 python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
Write a python program for the following question. Complete the symptom similarity function, which measures the...
Write a python program for the following question. Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed. def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int: '''Returns the similarity between symptoms_A and symptoms_B. symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent. The similarity measure is computed by the following equations: present_present + absent_absent - present_absent -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT