Python Forum

Full Version: Help with parameter variables losing value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
# 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,
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
(Feb-24-2018, 08:01 AM)Larz60+ Wrote: [ -> ]I run and get results
You probably run largecat's code, not the one from OP.
Yes, you're correct!