Python Forum
Help with parameter variables losing value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with parameter variables losing value
#1
Hello All,

I need help. Can someone tell me why I am not getting any results. Look like I am losing the variables values.
Here is my code.

# Global constant for Calories


CALORIES_FROM_FAT = 9
CALORIES_FROM_CARBS = 4

# main module

# Get grams fat

def main():
gramsFat = gramsCarbs = caloriesFat = caloriesCarbs = 0.0

getFat(gramsFat)
setFat(gramsFat,CALORIES_FROM_FAT)
getCarbs(gramsCarbs)
setCarbs(gramsCarbs, CALORIES_FROM_CARBS)
showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs)



# The getFat module gets weight and stores it
# in the inputFat reference variable.

def getFat(gramsFat):
gramsFat = float(input('Enter the number of fat grams consumed: '))


def setFat(gramsFat,CALORIES_FROM_FAT):
caloriesFat = float(gramsFat * CALORIES_FROM_FAT)


def getCarbs(gramsCarbs):
gramsCarbs = float(input("Enter carbohydrate grams consumed: "))




def setCarbs(gramsCarbs, CALORIES_FROM_CARBS):
caloriesCarbs = gramsCarbs * CALORIES_FROM_CARBS

# ------------------------------------------------------------------------
# The showCarbs function accepts the number of grams of fat and of carbs,
# as well as the calories from fat and from carbs, as arguments
# and displays the resulting calories

def showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs):
print("Grams of Fat: ", gramsFat)
print("Result calories: ", caloriesFat)
print("Grams of Carbs: ", gramsCarbs)
print("Result calories: ", caloriesCarbs)


# Call Main Function

main()
Reply
#2
# Global constant for Calories

CALORIES_FROM_FAT = 9
CALORIES_FROM_CARBS = 4

# main module
# Get grams fat

def main():
    gramsFat=getFat()
    caloriesFat=setFat(gramsFat,CALORIES_FROM_FAT)
    gramsCarbs=getCarbs()
    caloriesCarbs=setCarbs(gramsCarbs, CALORIES_FROM_CARBS)
    showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs)

# The getFat module gets weight and stores it
# in the inputFat reference variable.

def getFat():
    gramsFat = float(input('Enter the number of fat grams consumed: '))
    return gramsFat

def setFat(gramsFat,CALORIES_FROM_FAT):
    caloriesFat = float(gramsFat * CALORIES_FROM_FAT)
    return caloriesFat

def getCarbs():
    gramsCarbs = float(input("Enter carbohydrate grams consumed: "))
    return gramsCarbs

def setCarbs(gramsCarbs, CALORIES_FROM_CARBS):
    caloriesCarbs = gramsCarbs * CALORIES_FROM_CARBS 
    return caloriesCarbs

# ------------------------------------------------------------------------
# The showCarbs function accepts the number of grams of fat and of carbs, 
# as well as the calories from fat and from carbs, as arguments 
# and displays the resulting calories

def showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs): 
    print("Grams of Fat: ", gramsFat)
    print("Result calories: ", caloriesFat)
    print("Grams of Carbs: ", gramsCarbs)
    print("Result calories: ", caloriesCarbs)

# Call Main Function

main()

each function should have a return at the end,
Reply
#3
I run and get results:
Enter the number of fat grams consumed: 5
Enter carbohydrate grams consumed: 5
Grams of Fat:  5.0
Result calories:  45.0
Grams of Carbs:  5.0
Result calories:  20.0

Process finished with exit code 0
Reply
#4
(Feb-24-2018, 08:01 AM)Larz60+ Wrote: I run and get results
You probably run largecat's code, not the one from OP.
Reply
#5
Yes, you're correct!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,656 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Environment seems to keep losing references spacedog 2 1,869 Apr-23-2021, 07:36 PM
Last Post: spacedog
  losing memory antioch 1 1,735 Jan-20-2021, 05:29 AM
Last Post: antioch
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  SQL Alchemy dynamic class - declarative_base losing attributes mrdominikku 4 3,674 Jan-10-2020, 06:46 PM
Last Post: mrdominikku

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020