PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 25, 2022

[FIXED] What can I do to make my BMI calculator operable?

 November 25, 2022     local-variables, module, python, python-module     No comments   

Issue

I am trying to figure out how to use modules for a BMI program, but I keep getting issues with the variables and them either not being defined, or overwritten in a manner that would have them work themselves out properly.

Main code:

from getBMI import getBMI

# The Main Function
def main ():
    weight = input("What would you say your current weight is? ")
    weight = float(weight)
    BMI = 0
    getBMI()
    print("Your BMI is " + str(BMI))

# Calling the getBMI module
def getBMI():
    weight = 0
    BMI = weight * 703 / (weight * weight)

# Calling Main Function
main()

getBMI's code:

def main ():
    weight = input("What would you say your current weight is? ")

def getBMI():
    weight = 0
    BMI = weight * 703 / (weight * weight)

main()

I want to be able to let this work along with having the weight variable defined, or else the code will simply not work.


Solution

You are close, but there are some key points to know about using functions within python

# The Main Function
def main ():
    weight = input("What would you say your current weight is? ")
    weight = float(weight)
    BMI = getBMI(weight)
    print("Your BMI is " + str(BMI))

# Calling the getBMI module
def getBMI(weight):
    BMI = weight * 703 / (weight * weight)
    return BMI

# Calling Main Function
main()

I'm not sure of your logic, but based on your code I believe you should be able to alter your math if its not what you want.

Essentially we define the 2 functions then call the main function at the end.

The main function takes an input of weight and alters it to a float variable, then you set the BMI variable to a call of the getBMI function with a weight parameter (which is just your weight variable from earlier passed into it).

The getBMI function uses the weight variable that you passed to it from the main function and does the math for the BMI variable in the getBMI function. Once the math is completed you use the return option to send the results of BMI back to the BMI variable in the main function. after that you simply are printing the results of your BMI variable.



Answered By - ArchAngelPwn
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing