Python Forum
Help with parameter variables losing value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with parameter variables losing value (/thread-8521.html)



Help with parameter variables losing value - kofipython - Feb-24-2018

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()


RE: Help with parameter variables losing value - largecat - Feb-24-2018

# 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,


RE: Help with parameter variables losing value - Larz60+ - Feb-24-2018

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



RE: Help with parameter variables losing value - buran - Feb-24-2018

(Feb-24-2018, 08:01 AM)Larz60+ Wrote: I run and get results
You probably run largecat's code, not the one from OP.


RE: Help with parameter variables losing value - Larz60+ - Feb-24-2018

Yes, you're correct!